Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeigniter multiple file upload messes file extension
    primarykey
    data
    text
    <p>I am trying to get a multiple upload library working for my Codeigniter based website. I have it working almost but I have a slight problem if I upload more than one image, the file extentions get screwed up; for example, if I upload three JPEGS, I get this in my uploads folder,</p> <pre><code>image1.jpg image2.jpg.jpg image3.jpg.jpg.jpg </code></pre> <p>I cannot see what is causing it, this is the library in question and I am pretty sure that it is in there that the problem is, </p> <pre><code>&lt;?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 =&amp; 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 =&amp; get_instance(); // Is $_FILES[$field] set? If not, no reason to continue. if ( ! isset($_FILES[$field]['name'][0])) { $CI-&gt;upload-&gt;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-&gt;upload-&gt;validate_upload_path()) { // errors will already be set by validate_upload_path() so just return FALSE return FALSE; } for ($i=0; $i &lt; $num_files; $i++) { // $fname = $_FILES[$field]['name'][$i]; // echo "$fname\n\n&lt;br&gt;&lt;br&gt;\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-&gt;upload-&gt;file_temp = $_FILES[$field]['tmp_name'][$i]; $CI-&gt;upload-&gt;file_name = $CI-&gt;upload-&gt;_prep_filename($_FILES[$field]['name'][$i]); $CI-&gt;upload-&gt;file_size = $_FILES[$field]['size'][$i]; $CI-&gt;upload-&gt;file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]); $CI-&gt;upload-&gt;file_type = strtolower($CI-&gt;upload-&gt;file_type); $CI-&gt;upload-&gt;file_ext = $CI-&gt;upload-&gt;get_extension($_FILES[$field]['name'][$i]); // Convert the file size to kilobytes if ($CI-&gt;upload-&gt;file_size &gt; 0) { $CI-&gt;upload-&gt;file_size = round($CI-&gt;upload-&gt;file_size/1024, 2); } // Is the file type allowed to be uploaded? if ( ! $CI-&gt;upload-&gt;is_allowed_filetype()) { $error_hold[$i] = 'upload_invalid_filetype'; } // Is the file size within the allowed maximum? if ( ! $CI-&gt;upload-&gt;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-&gt;upload-&gt;is_allowed_dimensions()) { $error_hold[$i] = 'upload_invalid_dimensions'; } // Sanitize the file name for security $CI-&gt;upload-&gt;file_name = $CI-&gt;upload-&gt;clean_file_name($CI-&gt;upload-&gt;file_name); // Remove white spaces in the name if ($CI-&gt;upload-&gt;remove_spaces == TRUE) { $CI-&gt;upload-&gt;file_name = preg_replace("/\s+/", "_", $CI-&gt;upload-&gt;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-&gt;upload-&gt;orig_name = $CI-&gt;upload-&gt;file_name; if ($CI-&gt;upload-&gt;overwrite == FALSE) { $CI-&gt;upload-&gt;file_name = $CI-&gt;upload-&gt;set_filename($CI-&gt;upload-&gt;upload_path, $CI-&gt;upload-&gt;file_name); if ($CI-&gt;upload-&gt;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-&gt;upload-&gt;file_temp, $CI-&gt;upload-&gt;upload_path.$CI-&gt;upload-&gt;file_name)) { if ( ! @move_uploaded_file($CI-&gt;upload-&gt;file_temp, $CI-&gt;upload-&gt;upload_path.$CI-&gt;upload-&gt;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-&gt;upload-&gt;xss_clean == TRUE) { $CI-&gt;upload-&gt;do_xss_clean(); } if ($error_hold[$i]) { $error_upload = TRUE; // echo $error_hold[$i]; } else { if ($imageVar = $this-&gt;multiple_image_properties($CI-&gt;upload-&gt;upload_path.$CI-&gt;upload-&gt;file_name)) { $file_list[] = array( 'name' =&gt; $CI-&gt;upload-&gt;file_name, 'file' =&gt; $CI-&gt;upload-&gt;upload_path.$CI-&gt;upload-&gt;file_name, 'size' =&gt; $CI-&gt;upload-&gt;file_size, 'ext' =&gt; $CI-&gt;upload-&gt;file_ext, 'image_type' =&gt; $imageVar-&gt;image_type, 'height' =&gt; $imageVar-&gt;height, 'width' =&gt; $imageVar-&gt;width ); } else { $file_list[] = array( 'name' =&gt; $CI-&gt;upload-&gt;file_name, 'file' =&gt; $CI-&gt;upload-&gt;upload_path.$CI-&gt;upload-&gt;file_name, 'size' =&gt; $CI-&gt;upload-&gt;file_size, 'type' =&gt; $CI-&gt;upload-&gt;file_type, 'ext' =&gt; $CI-&gt;upload-&gt;file_ext, ); } } // For debugging /* if (strlen($error_hold[$i]) &gt; 1) { print_r($error_hold); } */ } // end for loop // Add error display for individual files if ($error_upload) { $this-&gt;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 =&amp; get_instance(); if ( ! $CI-&gt;upload-&gt;is_image()) { return false; } if (function_exists('getimagesize')) { if (FALSE !== ($D = @getimagesize($path))) { $types = array(1 =&gt; 'gif', 2 =&gt; 'jpeg', 3 =&gt; 'png'); $image-&gt;width = $D['0']; $image-&gt;height = $D['1']; $image-&gt;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 =&amp; get_instance(); $CI-&gt;lang-&gt;load('upload'); if (is_array($msg)) { foreach ($msg as $val) { $msg = ($CI-&gt;lang-&gt;line($val) == FALSE) ? $val : $CI-&gt;lang-&gt;line($val); $this-&gt;error_msg[] = $msg; log_message('error', $msg); } } else { $msg = ($CI-&gt;lang-&gt;line($msg) == FALSE) ? $msg : $CI-&gt;lang-&gt;line($msg); $this-&gt;error_msg[] = $msg; log_message('error', $msg); } } // -------------------------------------------------------------------- } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload