Why is that form validation still return an error even though the input file is not empty
Here's my controller together with my callback function
public function add_post()
{
$validation = array (
array(
'field' => 'post_title',
'label' => 'Post title',
'rules' => 'trim|required|alpha_numeric_spaces'
),
array(
'field' => 'post_desc',
'label' => 'Post Description',
'rules' => 'trim|required|alpha_numeric_spaces'
),
array(
'field' => 'post_content',
'label' => 'Post content',
'rules' => 'trim|required'
),
array(
'field' => 'headerimage',
'label' => 'File',
'rules' => 'required|callback_file_check'
)
);
$this->form_validation->set_rules($validation);
if($this->form_validation->run()===FALSE)
{
$info['errors'] = validation_errors();
$info['success'] = false;
}
else
{
$this->save_image();
$datetime = $this->get_time();
$data = array(
"post_title" => $this->input->post('post_title'),
"post_content" => $this->input->post('post_content'),
"post_image" => $this->uploadFileName,
"post_created" => $datetime
);
$this->Blog_model->add_post($data);
$info['success'] = true;
$info['message'] = "Successfully added blog post";
}
$this->output->set_content_type('application/json')->set_output(json_encode($info));
}
Here's the form mark-up
<?php echo form_open_multipart('#',array("class"=>"form-horizontal","id"=>"blogform")); ?>
<div class="row">
<div class="col-md-6">
<input type="text" placeholder="Enter your post title" name="post_title" class="form-control">
</div>
<div class="col-md-6 form-group">
<label for="file" class="control-label col-md-4">Select header image:</label>
<div class="col-md-8">
<input type="file" id="header" name="headerimage" accept="image/*" />
</div>
</div>
</div>
<input type="text" placeholder="Enter your post description" name="post_desc" class="form-control">
<label class="control-label text-muted">Post Body:</label>
<div>
<textarea id="post_content"></textarea>
</div>
<button class="btn btn-default pull-right" type="button" id="save-post"><i class="fa fa-save"></i> Save Post</button>
<div class="clearfix"></div>
<?php echo form_close(); ?>
I call the add_post controller function through AJAX.
$(document).on('click','#save-post',function(){
$post_content = $('#post_content').summernote('code');
$.ajax({
url:site_url('Blog/add_post'),
data: $('#blogform').serialize() + "&post_content=" + $post_content,
type: "POST",
dataType: 'json',
encode: true,
success: function(data){
if(!data.success){
if(data.errors){
$('#blog-message').html(data.errors).addClass('alert alert-danger');
}
}else{
alert(data.message);
}
}
});
});
I don't understand why validation is not working properly or I think the controller doesn't get the input file.
File upload data is not stored in the $_POST array, so cannot be validated using CodeIgniter's form_validation library. File uploads are available to PHP using the $_FILES array.
if (empty($_FILES['headerimage']['name']))
{
$this->form_validation->set_rules('headerimage', 'file', 'required');
// OR
$this->form_validation->set_rules('headerimage', 'file', 'trim|required');
}
OR you can use below system/application/libraries/MY_form_validation.php
Example :
$this->form_validation->set_rules(
// Field Name
$file_field_name ,
// Label
"YOUR FILE LAEBL",
// Rules
"file_required|file_min_size[10KB]|file_max_size[500KB]|file_allowed_type[jpg,jpeg]|file_image_mindim[50,50]|file_image_maxdim[400,300]"
);
MY_form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* Rules supported:
* file_required
* file_allowed_type[type]
* file_disallowed_type[type]
* file_size_min[size]
* file_size_max[size]
* file_image_mindim[x,y]
* file_image_maxdim[x,y]
*/
class MY_Form_validation extends CI_Form_validation {
function __construct()
{
parent::CI_Form_validation();
}
function set_rules($field, $label = '', $rules = '')
{
if(count($_POST)===0 AND count($_FILES) > 0)//it will prevent the form_validation from working
{
//add a dummy $_POST
$_POST['DUMMY_ITEM'] = '';
parent::set_rules($field,$label,$rules);
unset($_POST['DUMMY_ITEM']);
}
else
{
//we are safe just run as is
parent::set_rules($field,$label,$rules);
}
}
function run($group='')
{
$rc = FALSE;
log_message('DEBUG','called MY_form_validation:run()');
if(count($_POST)===0 AND count($_FILES)>0)//does it have a file only form?
{
//add a dummy $_POST
$_POST['DUMMY_ITEM'] = '';
$rc = parent::run($group);
unset($_POST['DUMMY_ITEM']);
}
else
{
//we are safe just run as is
$rc = parent::run($group);
}
return $rc;
}
function file_upload_error_message($error_code)
{
switch ($error_code)
{
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
default:
return 'Unknown upload error';
}
}
function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
log_message('DEBUG','called MY_form_validation::_execute ' . $row['field']);
//changed based on
//http://codeigniter.com/forums/viewthread/123816/P10/#619868
if(isset($_FILES[$row['field']]))
{// it is a file so process as a file
log_message('DEBUG','processing as a file');
$postdata = $_FILES[$row['field']];
//before doing anything check for errors
if($postdata['error'] !== UPLOAD_ERR_OK)
{
$this->_error_array[$row['field']] = $this->file_upload_error_message($postdata['error']);
return FALSE;
}
$_in_array = FALSE;
// If the field is blank, but NOT required, no further tests are necessary
$callback = FALSE;
if ( ! in_array('file_required', $rules) AND $postdata['size']==0)
{
// Before we bail out, does the rule contain a callback?
if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
{
$callback = TRUE;
$rules = (array('1' => $match[1]));
}
else
{
return;
}
}
foreach($rules as $rule)
{
/// COPIED FROM the original class
// Is the rule a callback?
$callback = FALSE;
if (substr($rule, 0, 9) == 'callback_')
{
$rule = substr($rule, 9);
$callback = TRUE;
}
// Strip the parameter (if exists) from the rule
// Rules can contain a parameter: max_length[5]
$param = FALSE;
if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
{
$rule = $match[1];
$param = $match[2];
}
// Call the function that corresponds to the rule
if ($callback === TRUE)
{
if ( ! method_exists($this->CI, $rule))
{
continue;
}
// Run the function and grab the result
$result = $this->CI->$rule($postdata, $param);
// Re-assign the result to the master data array
if ($_in_array == TRUE)
{
$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
}
else
{
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
}
// If the field isn't required and we just processed a callback we'll move on...
if ( ! in_array('file_required', $rules, TRUE) AND $result !== FALSE)
{
return;
}
}
else
{
if ( ! method_exists($this, $rule))
{
// If our own wrapper function doesn't exist we see if a native PHP function does.
// Users can use any native PHP function call that has one param.
if (function_exists($rule))
{
$result = $rule($postdata);
if ($_in_array == TRUE)
{
$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
}
else
{
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
}
}
continue;
}
$result = $this->$rule($postdata, $param);
if ($_in_array == TRUE)
{
$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
}
else
{
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
}
}
//this line needs testing !!!!!!!!!!!!! not sure if it will work
//it basically puts back the tested values back into $_FILES
//$_FILES[$row['field']] = $this->_field_data[$row['field']]['postdata'];
// Did the rule test negatively? If so, grab the error.
if ($result === FALSE)
{
if ( ! isset($this->_error_messages[$rule]))
{
if (FALSE === ($line = $this->CI->lang->line($rule)))
{
$line = 'Unable to access an error message corresponding to your field name.';
}
}
else
{
$line = $this->_error_messages[$rule];
}
// Is the parameter we are inserting into the error message the name
// of another field? If so we need to grab its "field label"
if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
{
$param = $this->_field_data[$param]['label'];
}
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
// Save the error message
$this->_field_data[$row['field']]['error'] = $message;
if ( ! isset($this->_error_array[$row['field']]))
{
$this->_error_array[$row['field']] = $message;
}
return;
}
}
}
else
{
log_message('DEBUG','Called parent _execute');
parent::_execute($row, $rules, $postdata,$cycles);
}
}
/**
* Future function. To return error message of choice.
* It will use $msg if it cannot find one in the lang files
*
* #param string $msg the error message
*/
function set_error($msg)
{
$CI =& get_instance();
$CI->lang->load('upload');
return ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
}
/**
* tests to see if a required file is uploaded
*
* #param mixed $file
*/
function file_required($file)
{
if($file['size']===0)
{
$this->set_message('file_required','Uploading a file for %s is required.');
return FALSE;
}
return TRUE;
}
/**
* tests to see if a file is within expected file size limit
*
* #param mixed $file
* #param mixed $max_size
*/
function file_size_max($file,$max_size)
{
$max_size_bit = $this->let_to_bit($max_size);
if($file['size']>$max_size_bit)
{
$this->set_message('file_size_max',"%s is too big. (max allowed is $max_size)");
return FALSE;
}
return true;
}
/**
* tests to see if a file is bigger than minimum size
*
* #param mixed $file
* #param mixed $min_size
*/
function file_size_min($file,$min_size)
{
$max_size_bit = $this->let_to_bit($max_size);
if($file['size']<$min_size_bit)
{
$this->set_message('file_size_min',"%s is too small. (Min allowed is $max_size)");
return FALSE;
}
return true;
}
/**
* tests the file extension for valid file types
*
* #param mixed $file
* #param mixed $type
*/
function file_allowed_type($file,$type)
{
//is type of format a,b,c,d? -> convert to array
$exts = explode(',',$type);
//is $type array? run self recursively
if(count($exts)>1)
{
foreach($exts as $v)
{
$rc = $this->file_allowed_type($file,$v);
if($rc===TRUE)
{
return TRUE;
}
}
}
//is type a group type? image, application, word_document, code, zip .... -> load proper array
$ext_groups = array();
$ext_groups['image'] = array('jpg','jpeg','gif','png');
$ext_groups['application'] = array('exe','dll','so','cgi');
$ext_groups['php_code'] = array('php','php4','php5','inc','phtml');
$ext_groups['word_document'] = array('rtf','doc','docx');
$ext_groups['compressed'] = array('zip','gzip','tar','gz');
if(array_key_exists($exts[0],$ext_groups))
{
$exts = $ext_groups[$exts[0]];
}
//get file ext
$file_ext = strtolower(strrchr($file['name'],'.'));
$file_ext = substr($file_ext,1);
if(!in_array($file_ext,$exts))
{
$this->set_message('file_allowed_type',"%s should be $type.");
return false;
}
else
{
return TRUE;
}
}
function file_disallowed_type($file,$type)
{
$rc = $this->file_allowed_type($file,$type);
if(!$rc)
{
$this->set_message('file_disallowed_type',"%s cannot be $type.");
}
return $rc;
}
//http://codeigniter.com/forums/viewthread/123816/P20/
/**
* given an string in format of ###AA converts to number of bits it is assignin
*
* #param string $sValue
* #return integer number of bits
*/
function let_to_bit($sValue)
{
// Split value from name
if(!preg_match('/([0-9]+)([ptgmkb]{1,2}|)/ui',$sValue,$aMatches))
{ // Invalid input
return FALSE;
}
if(empty($aMatches[2]))
{ // No name -> Enter default value
$aMatches[2] = 'KB';
}
if(strlen($aMatches[2]) == 1)
{ // Shorted name -> full name
$aMatches[2] .= 'B';
}
$iBit = (substr($aMatches[2], -1) == 'B') ? 1024 : 1000;
// Calculate bits:
switch(strtoupper(substr($aMatches[2],0,1)))
{
case 'P':
$aMatches[1] *= $iBit;
case 'T':
$aMatches[1] *= $iBit;
case 'G':
$aMatches[1] *= $iBit;
case 'M':
$aMatches[1] *= $iBit;
case 'K':
$aMatches[1] *= $iBit;
break;
}
// Return the value in bits
return $aMatches[1];
}
/**
* returns false if image is bigger than the dimensions given
*
* #param mixed $file
* #param array $dim
*/
function file_image_maxdim($file,$dim)
{
log_message('debug','MY_form_validation:file_image_maxdim ' . $dim);
$dim = explode(',',$dim);
if(count($dim)!==2)
{
//bad size given
$this->set_message('file_image_maxdim','%s has invalid rule expected similar to 150,300 .');
return FALSE;
}
log_message('debug','MY_form_validation:file_image_maxdim ' . $dim[0] . ' ' . $dim[1]);
//get image size
$d = $this->get_image_dimension($file['tmp_name']);
log_message('debug',$d[0] . ' ' . $d[1]);
if(!$d)
{
$this->set_message('file_image_maxdim','%s dimensions was not detected.');
return FALSE;
}
if($d[0] < $dim[0] && $d[1] < $dim[1])
{
return TRUE;
}
$this->set_message('file_image_maxdim','%s image size is too big.');
return FALSE;
}
/**
* returns false is the image is smaller than given dimension
*
* #param mixed $file
* #param array $dim
*/
function file_image_mindim($file,$dim)
{
$dim = explode(',',$dim);
if(count($dim)!==2)
{
//bad size given
$this->set_message('file_image_mindim','%s has invalid rule expected similar to 150,300 .');
return FALSE;
}
//get image size
$d = $this->get_image_dimension($file['tmp_name']);
if(!$d)
{
$this->set_message('file_image_mindim','%s dimensions was not detected.');
return FALSE;
}
log_message('debug',$d[0] . ' ' . $d[1]);
if($d[0] > $dim[0] && $d[1] > $dim[1])
{
return TRUE;
}
$this->set_message('file_image_mindim','%s image size is too big.');
return FALSE;
}
/**
* attempts to determine the image dimension
*
* #param mixed $file_name path to the image file
* #return array
*/
function get_image_dimension($file_name)
{
log_message('debug',$file_name);
if (function_exists('getimagesize'))
{
$D = #getimagesize($file_name);
return $D;
}
return FALSE;
}
}
/* End of file MY_form_validation.php */
/* Location: ./system/application/libraries/MY_form_validation.php */
For required file validation you can set validation as follow:
if (empty($_FILES['headerimage']['name']))
{
$this->form_validation->set_rules('headerimage', 'File', 'required');
}
Remove:
array(
'field' => 'headerimage',
'label' => 'File',
'rules' => 'required|callback_file_check'
)
Related
Thanks to this forum I came across below form validator some time back which work fine. However, I just have one problem.
When submitting a form with an empty textarea for instance it return the empty field as an error. However, as the value is not mandatory I need to correct this somehow.
<?php
/**
* Pork Formvalidator. validates fields by regexes and can sanatize them. Uses PHP filter_var built-in functions and extra regexes
* #package pork
*/
/**
* Pork.FormValidator
* Validates arrays or properties by setting up simple arrays
*
* #package pork
* #author SchizoDuckie
* #copyright SchizoDuckie 2009
* #version 1.0
* #access public
*/
class FormValidator
{
public static $regexes = Array(
'date' => "^[0-9]{4}[-/][0-9]{1,2}[-/][0-9]{1,2}\$",
'datetime' => "20\d{2}(-|\/)((0[1-9])|(1[0-2]))(-|\/)((0[1-9])|([1-2][0-9])|(3[0-1]))(T|\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])",
'amount' => "^[-]?[0-9]+\$",
'number' => "^[-]?[0-9,]+\$",
'alfanum' => "^[0-9a-zA-Z ,.-_\\s\?\!]+\$",
'not_empty' => "[a-z0-9A-Z]+",
'words' => "^[A-Za-z]+[A-Za-z \\s]*\$",
'phone' => "^[0-9]{10,11}\$",
'zipcode' => "^[1-9][0-9]{3}[a-zA-Z]{2}\$",
'plate' => "^([0-9a-zA-Z]{2}[-]){2}[0-9a-zA-Z]{2}\$",
'price' => "^[0-9.,]*(([.,][-])|([.,][0-9]{2}))?\$",
'2digitopt' => "^\d+(\,\d{2})?\$",
'2digitforce' => "^\d+\,\d\d\$",
'anything' => "^[\d\D]{1,}\$",
'username' => "^[\w]{3,32}\$"
);
private $validations, $sanatations, $mandatories, $equal, $errors, $corrects, $fields;
public function __construct($validations=array(), $mandatories = array(), $sanatations = array(), $equal=array())
{
$this->validations = $validations;
$this->sanatations = $sanatations;
$this->mandatories = $mandatories;
$this->equal = $equal;
$this->errors = array();
$this->corrects = array();
}
/**
* Validates an array of items (if needed) and returns true or false
*
* JP modofied this function so that it checks fields even if they are not submitted.
* for example the original code did not check for a mandatory field if it was not submitted.
* Also the types of non mandatory fields were not checked.
*/
public function validate($items)
{
$this->fields = $items;
$havefailures = false;
//Check for mandatories
foreach($this->mandatories as $key=>$val)
{
if(!array_key_exists($val,$items))
{
$havefailures = true;
$this->addError($val);
}
}
//Check for equal fields
foreach($this->equal as $key=>$val)
{
//check that the equals field exists
if(!array_key_exists($key,$items))
{
$havefailures = true;
$this->addError($val);
}
//check that the field it's supposed to equal exists
if(!array_key_exists($val,$items))
{
$havefailures = true;
$this->addError($val);
}
//Check that the two fields are equal
if($items[$key] != $items[$val])
{
$havefailures = true;
$this->addError($key);
}
}
foreach($this->validations as $key=>$val)
{
//An empty value or one that is not in the list of validations or one that is not in our list of mandatories
if(!array_key_exists($key,$items))
{
$this->addError($key, $val);
continue;
}
$result = self::validateItem($items[$key], $val);
if($result === false) {
$havefailures = true;
$this->addError($key, $val);
}
else
{
$this->corrects[] = $key;
}
}
return(!$havefailures);
}
/* JP
* Returns a JSON encoded array containing the names of fields with errors and those without.
*/
public function getJSON() {
$errors = array();
$correct = array();
if(!empty($this->errors))
{
foreach($this->errors as $key=>$val) { $errors[$key] = $val; }
}
if(!empty($this->corrects))
{
foreach($this->corrects as $key=>$val) { $correct[$key] = $val; }
}
$output = array('errors' => $errors, 'correct' => $correct);
return json_encode($output);
}
/**
*
* Sanatizes an array of items according to the $this->sanatations
* sanatations will be standard of type string, but can also be specified.
* For ease of use, this syntax is accepted:
* $sanatations = array('fieldname', 'otherfieldname'=>'float');
*/
public function sanatize($items)
{
foreach($items as $key=>$val)
{
if(array_search($key, $this->sanatations) === false && !array_key_exists($key, $this->sanatations)) continue;
$items[$key] = self::sanatizeItem($val, $this->validations[$key]);
}
return($items);
}
/**
*
* Adds an error to the errors array.
*/
private function addError($field, $type='string')
{
$this->errors[$field] = $type;
}
/**
*
* Sanatize a single var according to $type.
* Allows for static calling to allow simple sanatization
*/
public static function sanatizeItem($var, $type)
{
$flags = NULL;
switch($type)
{
case 'url':
$filter = FILTER_SANITIZE_URL;
break;
case 'int':
$filter = FILTER_SANITIZE_NUMBER_INT;
break;
case 'float':
$filter = FILTER_SANITIZE_NUMBER_FLOAT;
$flags = FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND;
break;
case 'email':
$var = substr($var, 0, 254);
$filter = FILTER_SANITIZE_EMAIL;
break;
case 'string':
default:
$filter = FILTER_SANITIZE_STRING;
$flags = FILTER_FLAG_NO_ENCODE_QUOTES;
break;
}
$output = filter_var($var, $filter, $flags);
return($output);
}
/**
*
* Validates a single var according to $type.
* Allows for static calling to allow simple validation.
*
*/
public static function validateItem($var, $type)
{
if(array_key_exists($type, self::$regexes))
{
$returnval = filter_var($var, FILTER_VALIDATE_REGEXP, array("options"=> array("regexp"=>'!'.self::$regexes[$type].'!i'))) !== false;
return($returnval);
}
$filter = false;
switch($type)
{
case 'email':
$var = substr($var, 0, 254);
$filter = FILTER_VALIDATE_EMAIL;
break;
case 'int':
$filter = FILTER_VALIDATE_INT;
break;
case 'boolean':
$filter = FILTER_VALIDATE_BOOLEAN;
break;
case 'ip':
$filter = FILTER_VALIDATE_IP;
break;
case 'url':
$filter = FILTER_VALIDATE_URL;
break;
}
return ($filter === false) ? false : filter_var($var, $filter) !== false ? true : false;
}
}
?>
So from what I understand I need to come up with a a way to validate an empty string as the above code will throw an error.
$validations = array(
'id' => 'number', //Value in _POST['id'] = '11'
'time' => 'datetime', //Value in _POST['time'] = '2016-03-17T11:05:01'
'description' => 'anything'); //Value in _POST['decription'] = ''
$required = array('id', 'time');
$validator = new FormValidator($validations, $required);
$validator->validate($_POST);
print_r $validator->getJSON();
You should either make the field required and add something like this
// validation
if (empty($variable)) {
echo 'such&such is required<br />';
$ok = false;
}
Or make the variable ="" or null;
I'm using Laravel 4 for a project, but got an issue. I'm not sure, what i'm doing wrong.
Details:
Form is posted to the controller's save function.
When validation fails, i'm redirecting to the create function
After redirect (using Redirect::to(somewhere)->withErrors($validator)->withInput()):
Validation errors are being displayed correctly (if any)
Input::old() is empty (it should contain previously submitted data)
Create function in controller
public function create()
{
$this->scripts[] = 'various js path here';
return View::make('admin.modules.events.create', array(
// Loading various scripts specified in this function
'scripts' => $this->scripts,
));
}
In the view:
...
{{ Form::bsInput('event_name', 'Event title', 'event title goes here', $error = (($errors->has('event_name')) ? $errors->get('event_name') : false), $type = 'text', Input::old('event_name')) }}
...
Note: bsInput is a wrapper around Form::Input() to create bootstrap controls together with labels
Controller:
public function save()
{
if (Input::has('submitEventSave'))
{
$event = Mihirevent::find(Input::get(event_id));
$event_add = false;
}
else
{
$event = new Mihirevent();
$event_add = true;
}
if ($event === false)
{
// doing something else
}
else
{
$event->event_name = Input::get('event_name');
$event->event_slug = Input::get('event_slug');
$event->event_description = Input::get('event_description');
$event->event_location_text = Input::get('event_location_text');
$event->event_location_data = Input::get('event_location_data');
$event->event_status = Input::get('event_status');
$event->featured_image = Input::get('featured_image');
$event->event_date_from = Input::get('event_date_from');
$event->event_date_until = Input::get('event_date_until');
$validation_rules = $event_add === true?$event->rules:$event->update_rules;
$inputs = array(
'event_name' => $event->event_name,
'event_slug' => $event->event_slug,
'event_location_text' => $event->event_location_text,
);
$validator = Validator::make($inputs, $validation_rules);
if ($validator->fails())
{
Input::flash();
if ($event_add === true)
{
return Redirect::to('admin/event/create')
->withErrors($validator)->withInput();
}
else
{
return Redirect::to('admin/event/edit/'.$event->event_id)
->withErrors($validator)->withInput();
}
}
// save
MihirEvent::save();
// redirect to list
return Redirect::route('adminEvent');
}
}
Update:
bsInput macro:
Form::macro('bsInput', function($name, $text, $placeholder = null, $error = false, $type = 'text', $default = null, $class=null)
{
$label = Form::label($name, $text, array('class' => 'control-label'));
$input = Form::input($type, $name, $default, array('placeholder' => $placeholder, 'class' => 'form-control'.($class?' '.$class:'')));
$error_messages = false;
if($error)
{
$error_messages = '<ol>';
foreach ($error as $value) {
$error_messages .= '<li>'.$value.'</li>';
}
$error_messages .= '</ol>';
}
$html = '<div class="form-group'.(($error) ? ' has-error' : '').'">';
$html .= $label;
$html .= $input;
$html .= (($error_messages) ? '<div class="alert alert-danger">'.$error_messages.'</div>' : '');
$html .= '</div>';
return $html;
});
Looking at the Laravel 4 source:
/**
* Flash an array of input to the session.
*
* #param array $input
* #return \Illuminate\Http\RedirectResponse
*/
public function withInput(array $input = null)
{
$input = $input ?: $this->request->input();
$this->session->flashInput($input);
return $this;
}
It looks like if you don't pass an array of Input with ->withInput it tries to pull it from the original request. Try modifying the line like so:
if ($event_add === true)
{
return Redirect::to('admin/event/create')
->withErrors($validator)->withInput(Input::all());
}
else
{
return Redirect::to('admin/event/edit/'.$event->event_id)
->withErrors($validator)->withInput(Input::all());
}
This should hopefully force it to pass the array of input values, instead of relying on the
'$this->request->input()'
still existing in the session.
You are doing Input::flash() and then withInput(), which effectively does Input::flash() twice, probably invalidating the flashed input. Try only doing one of the two.
Also, MihirEvent::save(); is wrong, you want to do $event->save();.
Finally i found the issue: There was a leading space in the routes.php file, before the <?php opening tag. (was working on a team and someone else added that space).
I'm trying to build some application but have frustated in uploded code with ci, I used multi_upload.
I won upload some file and send them with email, but on CI file_path must be uploaded in server root so I try uploaded and find the file_path.
Here is my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
function index()
{
$this->load->view('upload_form');
}
function do_upload()
{
$config['upload_path'] = './uploads/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '1000'; // in kb
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('Multi_upload');
$files = $this->multi_upload->go_upload();
if ( $files )
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array(
'upload_data' =>$this->upload->data()
);
$this->load->view('upload_success', $data);
}
}
}
?>
my upload_form:
<?php if (isset($error)) echo $error;?>
<form action="http://localhost/hemmmm/index.php/upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile[]" size="20" class="multi"
data-ajax="false"><br /><br />
<input type="submit" value="upload" />
My succes_form:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload More Files!'); ?></p>
</body>
</html>
my multi_upload library :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* This library assumes that you have already loaded the default CI Upload Library seperately
*
* Functions is based upon CI_Upload, Feel free to modify this
* library to function as an extension to CI_Upload
*
* Library modified by: Alvin Mites
* http://www.mitesdesign.com
*
*/
class Multi_upload {
function Multi_upload () {
// $CI =& get_instance();
}
/**
* Perform multiple file uploads
* Based upon JQuery Multiple Upload Class
* see http://www.fyneworks.com/jquery/multiple-file-upload/
*/
function go_upload($field = 'userfile') {
$CI =& get_instance();
// Is $_FILES[$field] set? If not, no reason to continue.
if ( ! isset($_FILES[$field]['name'][0]))
{
$CI->upload->set_error('upload_no_file_selected');
return FALSE;
} else
{
$num_files = count($_FILES[$field]['name']) -1;
$file_list = array();
$error_hold = array();
$error_upload = FALSE;
}
// Is the upload path valid?
if ( ! $CI->upload->validate_upload_path())
{
// errors will already be set by validate_upload_path() so just return FALSE
return FALSE;
}
for ($i=0; $i < $num_files; $i++) {
// $fname = $_FILES[$field]['name'][$i];
// echo "$fname\n\n<br><br>\n\n";
$error_hold[$i] = FALSE;
// Was the file able to be uploaded? If not, determine the reason why.
if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$i]))
{
$error = ( ! isset($_FILES[$field]['error'][$i])) ? 4 : $_FILES[$field]['error'][$i];
switch($error)
{
case 1: // UPLOAD_ERR_INI_SIZE
$error_hold[$i] = 'upload_file_exceeds_limit';
break;
case 2: // UPLOAD_ERR_FORM_SIZE
$error_hold[$i] = 'upload_file_exceeds_form_limit';
break;
case 3: // UPLOAD_ERR_PARTIAL
$error_hold[$i] = 'upload_file_partial';
break;
case 4: // UPLOAD_ERR_NO_FILE
$error_hold[$i] = 'upload_no_file_selected';
break;
case 6: // UPLOAD_ERR_NO_TMP_DIR
$error_hold[$i] = 'upload_no_temp_directory';
break;
case 7: // UPLOAD_ERR_CANT_WRITE
$error_hold[$i] = 'upload_unable_to_write_file';
break;
case 8: // UPLOAD_ERR_EXTENSION
$error_hold[$i] = 'upload_stopped_by_extension';
break;
default :
$error_hold[$i] = 'upload_no_file_selected';
break;
}
return FALSE;
}
// Set the uploaded data as class variables
$CI->upload->file_temp = $_FILES[$field]['tmp_name'][$i];
$CI->upload->file_name = $this->_prep_filename($_FILES[$field]['name'][$i]);
$CI->upload->file_size = $_FILES[$field]['size'][$i];
$CI->upload->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]);
$CI->upload->file_type = strtolower($CI->upload->file_type);
$CI->upload->file_ext = $CI->upload->get_extension($_FILES[$field]['name'][$i]);
// Convert the file size to kilobytes
if ($CI->upload->file_size > 0)
{
$CI->upload->file_size = round($CI->upload->file_size/1024, 2);
}
// Is the file type allowed to be uploaded?
if ( ! $CI->upload->is_allowed_filetype())
{
$error_hold[$i] = 'upload_invalid_filetype';
}
// Is the file size within the allowed maximum?
if ( ! $CI->upload->is_allowed_filesize())
{
$error_hold[$i] = 'upload_invalid_filesize';
}
// Are the image dimensions within the allowed size?
// Note: This can fail if the server has an open_basdir restriction.
if ( ! $CI->upload->is_allowed_dimensions())
{
$error_hold[$i] = 'upload_invalid_dimensions';
}
// Sanitize the file name for security
$CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name);
// Remove white spaces in the name
if ($CI->upload->remove_spaces == TRUE)
{
$CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name);
}
/*
* Validate the file name
* This function appends an number onto the end of
* the file if one with the same name already exists.
* If it returns false there was a problem.
*/
$CI->upload->orig_name = $CI->upload->file_name;
if ($CI->upload->overwrite == FALSE)
{
$CI->upload->file_name = $CI->upload->set_filename($CI->upload->upload_path, $CI->upload->file_name);
if ($CI->upload->file_name === FALSE)
{
$error_hold[$i] = TRUE;
}
}
/*
* Move the file to the final destination
* To deal with different server configurations
* we'll attempt to use copy() first. If that fails
* we'll use move_uploaded_file(). One of the two should
* reliably work in most environments
*/
if ( ! #copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
{
if ( ! #move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
{
$error_hold[$i] = 'upload_destination_error';
}
}
/*
* Run the file through the XSS hacking filter
* This helps prevent malicious code from being
* embedded within a file. Scripts can easily
* be disguised as images or other file types.
*/
if ($CI->upload->xss_clean == TRUE)
{
$CI->upload->do_xss_clean();
}
if ($error_hold[$i]) {
$error_upload = TRUE;
// echo $error_hold[$i];
} else {
if ($imageVar = $this->multiple_image_properties($CI->upload->upload_path.$CI->upload->file_name)) {
$file_list[] = array(
'name' => $CI->upload->file_name,
'file' => $CI->upload->upload_path.$CI->upload->file_name,
'size' => $CI->upload->file_size,
'ext' => $CI->upload->file_ext,
'image_type' => $imageVar->image_type,
'height' => $imageVar->height,
'width' => $imageVar->width
);
} else {
$file_list[] = array(
'name' => $CI->upload->file_name,
'file' => $CI->upload->upload_path.$CI->upload->file_name,
'size' => $CI->upload->file_size,
'type' => $CI->upload->file_type,
'ext' => $CI->upload->file_ext,
);
}
}
// For debugging
/*
if (strlen($error_hold[$i]) > 1) {
print_r($error_hold);
}
*/
} // end for loop
// Add error display for individual files
if ($error_upload) {
$this->set_error($error_hold);
return FALSE;
} else {
return $file_list;
}
}
// --------------------------------------------------------------------
/**
* Set Image Properties
*
* Uses GD to determine the width/height/type of image
*
* #access public
* #param string
* #return void
*/
function multiple_image_properties($path = '')
{
$CI =& get_instance();
if ( ! $CI->upload->is_image())
{
return false;
}
if (function_exists('getimagesize'))
{
if (FALSE !== ($D = #getimagesize($path)))
{
$types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
$image->width = $D['0'];
$image->height = $D['1'];
$image->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
return $image;
}
}
}
// --------------------------------------------------------------------
/**
* Set an error message
*
* #access public
* #param string
* #return void
*/
function set_error($msg)
{
$CI =& get_instance();
$CI->lang->load('upload');
if (is_array($msg))
{
foreach ($msg as $val)
{
$msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
$this->error_msg[] = $msg;
log_message('error', $msg);
}
}
else
{
$msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
$this->error_msg[] = $msg;
log_message('error', $msg);
}
}
// --------------------------------------------------------------------
protected function _prep_filename($filename) {
$CI = & get_instance();
if (strpos($filename, '.') === FALSE OR $CI->upload->allowed_types == '*') {
return $filename;
}
$parts = explode('.', $filename);
$ext = array_pop($parts);
$filename = array_shift($parts);
foreach ($parts as $part) {
if (!in_array(strtolower($part), $CI->upload->allowed_types) OR $CI->upload->mimes_types(strtolower($part)) === FALSE) {
$filename .= '.' . $part . '_';
} else {
$filename .= '.' . $part;
}
}
$filename .= '.' . $ext;
return $filename;
}
}
?>
When I try to upload one file it works and I have great explaination about upload_data
but when I try to upload multiple files it works the file is uploaded, but just displays
status about last file uploaded.
$file_list array is being overwritten every time it processes an image from the looks of it. Try incrementing that with the $i variable when it's being populated.
$file_list[$i] = array
from my below code i am trying to upload 4 images, but i am only able to upload last image to my upload folder.
My image validation and all the other task is working fine. But only i have problem with the multiple images to upload into its distinction folder.
Please check my below code and help to solve my problem.
MyController.php
class Booksetups extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
$this->load->model('Booksmodel');
$this->load->library('form_validation');
$this->load->library('pagination');
}
public function valid_upload()
{
$this->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 2048;
$config['max_width'] = 385;
$config['max_height'] = 410;
$this->upload->initialize($config);
if (!$this->upload->validate_upload('img1') || !$this->upload->validate_upload('img2') || !$this->upload->validate_upload('img3') || !$this->upload->validate_upload('img4') ) {
$this->form_validation->set_message('valid_upload', $this->upload->display_errors());
return FALSE;
} else {
return TRUE;
}
}
function book($book_id = 0)
{
$config = array();
$config['base_url'] = 'http://localhost/thebestbookfinder.com/Booksetups/book/pgn/';
$config["total_rows"] = $this->Booksmodel->record_count();
$config['uri_segment'] = 4;
$config['per_page'] = 17;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('book_title', '"Title"','trim|required|min_length[4]|max_length[150]|xss_clean');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('edition_id', '"Edition Name"','trim|min_length[1]|max_length[150]|xss_clean');
//---------------------validating images---------------------------
$this->form_validation->set_rules('img1', 'Image 1', 'trim|xss_clean|callback_valid_upload');
$this->form_validation->set_rules('img2', 'Image 2', 'trim|xss_clean|callback_valid_upload');
$this->form_validation->set_rules('img3', 'Image 3', 'trim|xss_clean|callback_valid_upload');
$this->form_validation->set_rules('img4', 'Image 4', 'trim|xss_clean|callback_valid_upload');
//-----------Here i am uploading my images into my specified folder-------------------------------
if ($this->form_validation->run() === TRUE) {
$this->upload->do_upload('img1');
$this->upload->do_upload('img2');
$this->upload->do_upload('img3');
$this->upload->do_upload('img4');
$this->Booksmodel->entry_insert();
$this->session->set_flashdata('msg', '1 row(s) inserted.');
redirect(current_url());
}
................
................
My_Upload.php
Class My_Upload extends CI_Upload
{
public function __construct(){
parent::__construct();
}
public function validate_upload($field = 'img1')
{
// Is $_FILES[$field] set? If not, no reason to continue.
if (! isset($_FILES[$field])) {
$this->set_error('upload_no_file_selected');
return FALSE;
}
// Is the upload path valid?
if (! $this->validate_upload_path()) {
// errors will already be set by validate_upload_path() so just return FALSE
return FALSE;
}
// Was the file able to be uploaded? If not, determine the reason why.
if (! is_uploaded_file($_FILES[$field]['tmp_name'])) {
$error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
switch ($error) {
case 1:
// UPLOAD_ERR_INI_SIZE
$this->set_error('upload_file_exceeds_limit');
break;
case 2:
// UPLOAD_ERR_FORM_SIZE
$this->set_error('upload_file_exceeds_form_limit');
break;
case 3:
// UPLOAD_ERR_PARTIAL
$this->set_error('upload_file_partial');
break;
case 4:
// UPLOAD_ERR_NO_FILE
$this->set_error('upload_no_file_selected');
break;
case 6:
// UPLOAD_ERR_NO_TMP_DIR
$this->set_error('upload_no_temp_directory');
break;
case 7:
// UPLOAD_ERR_CANT_WRITE
$this->set_error('upload_unable_to_write_file');
break;
case 8:
// UPLOAD_ERR_EXTENSION
$this->set_error('upload_stopped_by_extension');
break;
default : $this->set_error('upload_no_file_selected');
break;
}
return FALSE;
}
// Set the uploaded data as class variables
$this->file_temp = $_FILES[$field]['tmp_name'];
$this->file_size = $_FILES[$field]['size'];
$this->_file_mime_type($_FILES[$field]);
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
$this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
$this->file_name = $this->_prep_filename($_FILES[$field]['name']);
$this->file_ext = $this->get_extension($this->file_name);
$this->client_name = $this->file_name;
// Is the file type allowed to be uploaded?
if (! $this->is_allowed_filetype()) {
$this->set_error('upload_invalid_filetype');
return FALSE;
}
// if we're overriding, let's now make sure the new name and type is allowed
if ($this->_file_name_override != '') {
$this->file_name = $this->_prep_filename($this->_file_name_override);
// If no extension was provided in the file_name config item, use the uploaded one
if (strpos($this->_file_name_override, '.') === FALSE) {
$this->file_name .= $this->file_ext;
}
// An extension was provided, lets have it!
else
{
$this->file_ext = $this->get_extension($this->_file_name_override);
}
if (! $this->is_allowed_filetype(TRUE)) {
$this->set_error('upload_invalid_filetype');
return FALSE;
}
}
// Convert the file size to kilobytes
if ($this->file_size > 0) {
$this->file_size = round($this->file_size/1024, 2);
}
// Is the file size within the allowed maximum?
if (! $this->is_allowed_filesize()) {
$this->set_error('upload_invalid_filesize');
return FALSE;
}
// Are the image dimensions within the allowed size?
// Note: This can fail if the server has an open_basdir restriction.
if (! $this->is_allowed_dimensions()) {
$this->set_error('upload_invalid_dimensions');
return FALSE;
}
// Sanitize the file name for security
$this->file_name = $this->clean_file_name($this->file_name);
// Truncate the file name if it's too long
if ($this->max_filename > 0) {
$this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
}
// Remove white spaces in the name
if ($this->remove_spaces == TRUE) {
$this->file_name = preg_replace("/\s+/", "_", $this->file_name);
}
/*
* Validate the file name
* This function appends an number onto the end of
* the file if one with the same name already exists.
* If it returns false there was a problem.
*/
$this->orig_name = $this->file_name;
if ($this->overwrite == FALSE) {
$this->file_name = $this->set_filename($this->upload_path, $this->file_name);
if ($this->file_name === FALSE) {
return FALSE;
}
}
/*
* Run the file through the XSS hacking filter
* This helps prevent malicious code from being
* embedded within a file. Scripts can easily
* be disguised as images or other file types.
*/
if ($this->xss_clean) {
if ($this->do_xss_clean() === FALSE) {
$this->set_error('upload_unable_to_write_file');
return FALSE;
}
}
$this->set_image_properties($this->upload_path.$this->file_name);
return TRUE;
}
public function do_upload($field = 'img1')
{
/*
* Move the file to the final destination
* To deal with different server configurations
* we'll attempt to use copy() first. If that fails
* we'll use move_uploaded_file(). One of the two should
* reliably work in most environments
*/
if (! #copy($this->file_temp, $this->upload_path.$this->file_name)) {
if (! #move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)) {
$this->set_error('upload_destination_error');
return FALSE;
}
}
/*
* Set the finalized image dimensions
* This sets the image width/height (assuming the
* file was an image). We use this information
* in the "data" function.
*/
return TRUE;
}
}
MyViewForm.php
<?php echo form_open_multipart('Booksetups/book');?>
<?php echo form_hidden('book_id',$fbook_id['value']);?>
<input type="file" name="img1" />
<input type="file" name="img2" />
<input type="file" name="img3" />
<input type="file" name="img4" />
<?php
echo form_submit($submitbtn);
echo form_reset($resetbtn);
?>
<?php echo form_close();?>
Use uploadify jquery plugin.Its easy to use and fully customized with real time progress bar and many more features.
Uploadify demo
Debug your do_upload function, you are not using local variable $ field anywhere
i have a error to use of multiupload with CodeIgniter:
What changes can, to be problem i solve?
waht do i do?
With respect
this is error:
Fatal error: Call to protected method CI_Upload::_prep_filename() from
context 'Multi_upload' in
D:\xampp\htdocs\CodeIgniter_2.0.0\application\libraries\Multi_upload.php
on line 91
this full my class, Line 91 is indicated:
class Multi_upload {
function Multi_upload () {
// $CI =& get_instance();
}
/**
* Perform multiple file uploads
* Based upon JQuery Multiple Upload Class
* see http://www.fyneworks.com/jquery/multiple-file-upload/
*/
function go_upload($field = 'userfile') {
$CI =& get_instance();
// Is $_FILES[$field] set? If not, no reason to continue.
if ( ! isset($_FILES[$field]['name'][0]))
{
$CI->upload->set_error('upload_no_file_selected');
return FALSE;
} else
{
$num_files = count($_FILES[$field]['name']) -1;
$file_list = array();
$error_hold = array();
$error_upload = FALSE;
}
// Is the upload path valid?
if ( ! $CI->upload->validate_upload_path())
{
// errors will already be set by validate_upload_path() so just return FALSE
return FALSE;
}
for ($i=0; $i < $num_files; $i++) {
// $fname = $_FILES[$field]['name'][$i];
// echo "$fname\n\n<br><br>\n\n";
$error_hold[$i] = FALSE;
// Was the file able to be uploaded? If not, determine the reason why.
if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$i]))
{
$error = ( ! isset($_FILES[$field]['error'][$i])) ? 4 : $_FILES[$field]['error'][$i];
switch($error)
{
case 1: // UPLOAD_ERR_INI_SIZE
$error_hold[$i] = 'upload_file_exceeds_limit';
break;
case 2: // UPLOAD_ERR_FORM_SIZE
$error_hold[$i] = 'upload_file_exceeds_form_limit';
break;
case 3: // UPLOAD_ERR_PARTIAL
$error_hold[$i] = 'upload_file_partial';
break;
case 4: // UPLOAD_ERR_NO_FILE
$error_hold[$i] = 'upload_no_file_selected';
break;
case 6: // UPLOAD_ERR_NO_TMP_DIR
$error_hold[$i] = 'upload_no_temp_directory';
break;
case 7: // UPLOAD_ERR_CANT_WRITE
$error_hold[$i] = 'upload_unable_to_write_file';
break;
case 8: // UPLOAD_ERR_EXTENSION
$error_hold[$i] = 'upload_stopped_by_extension';
break;
default :
$error_hold[$i] = 'upload_no_file_selected';
break;
}
return FALSE;
}
// Set the uploaded data as class variables
$CI->upload->file_temp = $_FILES[$field]['tmp_name'][$i];
////////////////////////////////////here - line 91/////////////////////////////////
$CI->upload->file_name = $CI->upload->_prep_filename($_FILES[$field]['name'][$i]); // this is line 91
//////////////////////////////////////////////////////////////////////////////////
$CI->upload->file_size = $_FILES[$field]['size'][$i];
$CI->upload->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]);
$CI->upload->file_type = strtolower($CI->upload->file_type);
$CI->upload->file_ext = $CI->upload->get_extension($_FILES[$field]['name'][$i]);
// Convert the file size to kilobytes
if ($CI->upload->file_size > 0)
{
$CI->upload->file_size = round($CI->upload->file_size/1024, 2);
}
// Is the file type allowed to be uploaded?
if ( ! $CI->upload->is_allowed_filetype())
{
$error_hold[$i] = 'upload_invalid_filetype';
}
// Is the file size within the allowed maximum?
if ( ! $CI->upload->is_allowed_filesize())
{
$error_hold[$i] = 'upload_invalid_filesize';
}
// Are the image dimensions within the allowed size?
// Note: This can fail if the server has an open_basdir restriction.
if ( ! $CI->upload->is_allowed_dimensions())
{
$error_hold[$i] = 'upload_invalid_dimensions';
}
// Sanitize the file name for security
$CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name);
// Remove white spaces in the name
if ($CI->upload->remove_spaces == TRUE)
{
$CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name);
}
/*
* Validate the file name
* This function appends an number onto the end of
* the file if one with the same name already exists.
* If it returns false there was a problem.
*/
$CI->upload->orig_name = $CI->upload->file_name;
if ($CI->upload->overwrite == FALSE)
{
$CI->upload->file_name = $CI->upload->set_filename($CI->upload->upload_path, $CI->upload->file_name);
if ($CI->upload->file_name === FALSE)
{
$error_hold[$i] = TRUE;
}
}
/*
* Move the file to the final destination
* To deal with different server configurations
* we'll attempt to use copy() first. If that fails
* we'll use move_uploaded_file(). One of the two should
* reliably work in most environments
*/
if ( ! #copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
{
if ( ! #move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
{
$error_hold[$i] = 'upload_destination_error';
}
}
/*
* Run the file through the XSS hacking filter
* This helps prevent malicious code from being
* embedded within a file. Scripts can easily
* be disguised as images or other file types.
*/
if ($CI->upload->xss_clean == TRUE)
{
$CI->upload->do_xss_clean();
}
if ($error_hold[$i]) {
$error_upload = TRUE;
// echo $error_hold[$i];
} else {
if ($imageVar = $this->multiple_image_properties($CI->upload->upload_path.$CI->upload->file_name)) {
$file_list[] = array(
'name' => $CI->upload->file_name,
'file' => $CI->upload->upload_path.$CI->upload->file_name,
'size' => $CI->upload->file_size,
'ext' => $CI->upload->file_ext,
'image_type' => $imageVar->image_type,
'height' => $imageVar->height,
'width' => $imageVar->width
);
} else {
$file_list[] = array(
'name' => $CI->upload->file_name,
'file' => $CI->upload->upload_path.$CI->upload->file_name,
'size' => $CI->upload->file_size,
'type' => $CI->upload->file_type,
'ext' => $CI->upload->file_ext,
);
}
}
// For debugging
/*
if (strlen($error_hold[$i]) > 1) {
print_r($error_hold);
}
*/
} // end for loop
// Add error display for individual files
if ($error_upload) {
$this->set_error($error_hold);
return FALSE;
} else {
return $file_list;
}
}
// --------------------------------------------------------------------
/**
* Set Image Properties
*
* Uses GD to determine the width/height/type of image
*
* #access public
* #param string
* #return void
*/
function multiple_image_properties($path = '')
{
$CI =& get_instance();
if ( ! $CI->upload->is_image())
{
return false;
}
if (function_exists('getimagesize'))
{
if (FALSE !== ($D = #getimagesize($path)))
{
$types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
$image->width = $D['0'];
$image->height = $D['1'];
$image->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
return $image;
}
}
}
// --------------------------------------------------------------------
/**
* Set an error message
*
* #access public
* #param string
* #return void
*/
function set_error($msg)
{
$CI =& get_instance();
$CI->lang->load('upload');
if (is_array($msg))
{
foreach ($msg as $val)
{
$msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
$this->error_msg[] = $msg;
log_message('error', $msg);
}
}
else
{
$msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
$this->error_msg[] = $msg;
log_message('error', $msg);
}
}
// --------------------------------------------------------------------
}
?>
I read the library and I think this may help. You must replace the line 91 with this:
$CI->upload->file_name = $_FILES[$field]['name'][$i];
I hope you find it useful.
CI_Upload::_prep_filename() is a protected method. You cannot call it directly outside of the class.