Multiple file uploads just displays last uploaded file - php

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

Related

Codeigniter File Validation Not Working

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'
)

unable to upload multiple images to its upload folder

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

how can convert $files contain to array?

How do I solve this error?
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/tour.php
Line Number: 81
this is line 81:
$files = $this->multi_upload->go_upload();
var_dump($files);
$images = array();
foreach ($files as $img) { //line 81
$images[] = $img['file'];
}
this my $files in top code:
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 = $_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;
}
}
The simple answer is that $files is not an array. foreach only works on arrays. We don't know what the value is, but in any case, it's not right. You'd have to look at the go_upload function to see why it's not doing what you expect.
Actually doing like this you wil get the file or the value you want to.
foreach ($files as $img) { //line 81
echo $img; // echo the Var and you will see..
}
This line -> $files = $this->multi_upload->go_upload(); probably come from a file object in your form, right? Then it must to be named like this file[] and not only file. You need to be specific that it is an Array. Otherwise nothing will happen.
Too avoid seeing this warning message you should count the array:
if (count($files) > 0)
{
foreach ($files as $img) { //line 81
$images[] = $img['file'];
}
}
Regarding the 'Invalid argument supplied for foreach()' error this is probably because $files is not an array (or similar). The best way to do this IMHO is:
$files = $this->multi_upload->go_upload();
echo '<pre>';
echo "Files:\n";
print_r($files);
echo '</pre>';
$images = array();
foreach ($files as $img) { //this is line 80
$images[] = $img['file'];
}
if ( ! $files )
{
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('admin/tour/insert_foreign');
}
Please make this change and show us the output.
I figure I would just share how I handle multiple uploads in CI, it's a little different than your method:
Form looks like this:
<?php echo form_open_multipart('upload/multiple_upload');?>
<input type="file" name="userfile[]" size="20" /><br />
<input type="file" name="userfile[]" size="20" /><br />
</form>
Upload/multiple_upload function in controller looks like this:
function multiple_upload()
{
$config['upload_path'] = './userpics/originals/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '512'; // in kb
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('myupload');
$error = array(); $data = array();
for ($i=0;$i<count($_FILES['userfile']['name']);$i++) {
$this->myupload->initialize($config);
if (!$this->myupload->do_upload('userfile',$i)) {
$error[] = $this->myupload->display_errors();
}
$data[$i] = $this->myupload->data(); //gradually build up upload->data()
}
if ( count($error) > 0 )
{
print_r($error);
}
else
{
print_r($data);
}
}
At this point I made a copy of CI upload class, and pasted it into my applications library directory. A few changes need to be made. I named it myupload.
# Line 143:
public function do_upload($field = 'userfile')
change to
public function do_upload($field = 'userfile', $i = 0)
Any lines in this function above line 200, you must add [$i] to the end of the $_FILES variables.
Example:
is_uploaded_file($_FILES[$field]['tmp_name'])
change to:
is_uploaded_file($_FILES[$field]['tmp_name'][$i])
There are 9 lines to be updated.
Are you sure that you get an array? is see several return FALSE. So you should check,
if ($files !== FALSE)
or casting it to an (empty) array (if false returns) with
$files = (array) $this->multi_upload->go_upload();

a error to use of multiupload with CodeIgniter

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.

Invalid argument supplied for foreach() [duplicate]

How do I solve this error?
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/tour.php
Line Number: 81
this is line 81:
$files = $this->multi_upload->go_upload();
var_dump($files);
$images = array();
foreach ($files as $img) { //line 81
$images[] = $img['file'];
}
this my $files in top code:
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 = $_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;
}
}
The simple answer is that $files is not an array. foreach only works on arrays. We don't know what the value is, but in any case, it's not right. You'd have to look at the go_upload function to see why it's not doing what you expect.
Actually doing like this you wil get the file or the value you want to.
foreach ($files as $img) { //line 81
echo $img; // echo the Var and you will see..
}
This line -> $files = $this->multi_upload->go_upload(); probably come from a file object in your form, right? Then it must to be named like this file[] and not only file. You need to be specific that it is an Array. Otherwise nothing will happen.
Too avoid seeing this warning message you should count the array:
if (count($files) > 0)
{
foreach ($files as $img) { //line 81
$images[] = $img['file'];
}
}
Regarding the 'Invalid argument supplied for foreach()' error this is probably because $files is not an array (or similar). The best way to do this IMHO is:
$files = $this->multi_upload->go_upload();
echo '<pre>';
echo "Files:\n";
print_r($files);
echo '</pre>';
$images = array();
foreach ($files as $img) { //this is line 80
$images[] = $img['file'];
}
if ( ! $files )
{
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('admin/tour/insert_foreign');
}
Please make this change and show us the output.
I figure I would just share how I handle multiple uploads in CI, it's a little different than your method:
Form looks like this:
<?php echo form_open_multipart('upload/multiple_upload');?>
<input type="file" name="userfile[]" size="20" /><br />
<input type="file" name="userfile[]" size="20" /><br />
</form>
Upload/multiple_upload function in controller looks like this:
function multiple_upload()
{
$config['upload_path'] = './userpics/originals/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '512'; // in kb
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('myupload');
$error = array(); $data = array();
for ($i=0;$i<count($_FILES['userfile']['name']);$i++) {
$this->myupload->initialize($config);
if (!$this->myupload->do_upload('userfile',$i)) {
$error[] = $this->myupload->display_errors();
}
$data[$i] = $this->myupload->data(); //gradually build up upload->data()
}
if ( count($error) > 0 )
{
print_r($error);
}
else
{
print_r($data);
}
}
At this point I made a copy of CI upload class, and pasted it into my applications library directory. A few changes need to be made. I named it myupload.
# Line 143:
public function do_upload($field = 'userfile')
change to
public function do_upload($field = 'userfile', $i = 0)
Any lines in this function above line 200, you must add [$i] to the end of the $_FILES variables.
Example:
is_uploaded_file($_FILES[$field]['tmp_name'])
change to:
is_uploaded_file($_FILES[$field]['tmp_name'][$i])
There are 9 lines to be updated.
Are you sure that you get an array? is see several return FALSE. So you should check,
if ($files !== FALSE)
or casting it to an (empty) array (if false returns) with
$files = (array) $this->multi_upload->go_upload();

Categories