Advertisement
Guest User

CodeIgniter - MY_upload - upload multiple files

a guest
Aug 14th, 2013
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.36 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3.     /*
  4.     Class: MY_Upload (Helper)
  5.  
  6.     Description:
  7.         A helper class that extends the CI_Upload class.
  8.     */
  9.     class MY_Upload extends CI_Upload {
  10.  
  11.         public $ip_address = '';
  12.         /*
  13.             Function: upload_files()
  14.  
  15.             Description:
  16.                 Uploads the files.
  17.  
  18.             Parameters:
  19.                 $files - An array files we are trying to upload
  20.  
  21.             Returns:
  22.                 Object -> status
  23.                         TRUE - If all the files have been uploaded successfully
  24.                         FALSE - IF nothing was uploaded or there was an error
  25.                 Object -> file_info
  26.                         An array containing the file information as returned from this->data
  27.  
  28.             See Also:
  29.                 <do_upload_array>
  30.                 <data>
  31.         */
  32.         public function upload_files($files)
  33.         {
  34.             $response = new stdClass();
  35.             $response->status = FALSE;
  36.  
  37.             if (isset($files))
  38.             {
  39.                 foreach ($files as $field => $file)
  40.                 {
  41.                     foreach($file as $key => $config)
  42.                     {
  43.                         $this->initialize($config);
  44.                         $upload = $this->do_upload_array($field, $key);
  45.  
  46.                             if($upload->status)
  47.                             {
  48.                                 $response->status = TRUE;
  49.                                 $response->file_info[] = $upload->file_info;
  50.                             }else{
  51.                                 $response->status = FALSE;
  52.                             }
  53.                     }
  54.                 }
  55.             }
  56.             return $response;
  57.         }
  58.  
  59.  
  60.         // --------------------------------------------------------------------
  61.  
  62.         /**
  63.          * Initialize preferences
  64.          *
  65.          * @param   array
  66.          * @return  void
  67.          */
  68.         public function initialize($config = array())
  69.         {
  70.             $defaults = array(
  71.                 'max_size'          => 0,
  72.                 'max_width'         => 0,
  73.                 'max_height'        => 0,
  74.                 'max_filename'      => 0,
  75.                 'allowed_types'     => "",
  76.                 'file_temp'         => "",
  77.                 'file_name'         => "",
  78.                 'orig_name'         => "",
  79.                 'file_type'         => "",
  80.                 'file_size'         => "",
  81.                 'file_ext'          => "",
  82.                 'upload_path'       => "",
  83.                 'overwrite'         => FALSE,
  84.                 'encrypt_name'      => FALSE,
  85.                 'is_image'          => FALSE,
  86.                 'image_width'       => '',
  87.                 'image_height'      => '',
  88.                 'image_type'        => '',
  89.                 'image_size_str'    => '',
  90.                 'error_msg'         => array(),
  91.                 'mimes'             => array(),
  92.                 'remove_spaces'     => TRUE,
  93.                 'xss_clean'         => FALSE,
  94.                 'temp_prefix'       => "temp_file_",
  95.                 'client_name'       => '',
  96.                 'ip_address'         => $_SERVER['REMOTE_ADDR']
  97.             );
  98.  
  99.  
  100.             foreach ($defaults as $key => $val)
  101.             {
  102.                 if (isset($config[$key]))
  103.                 {
  104.                     $method = 'set_'.$key;
  105.                     if (method_exists($this, $method))
  106.                     {
  107.                         $this->$method($config[$key]);
  108.                     }
  109.                     else
  110.                     {
  111.                         $this->$key = $config[$key];
  112.                     }
  113.                 }
  114.                 else
  115.                 {
  116.                     $this->$key = $val;
  117.                 }
  118.             }
  119.  
  120.             // if a file_name was provided in the config, use it instead of the user input
  121.             // supplied file name for all uploads until initialized again
  122.             $this->_file_name_override = $this->file_name;
  123.         }
  124.  
  125.         // --------------------------------------------------------------------
  126.  
  127.         /**
  128.          * Finalized Data Array
  129.          *
  130.          * Returns an associative array containing all of the information
  131.          * related to the upload, allowing the developer easy access in one array.
  132.          *
  133.          * @return  array
  134.          */
  135.         public function data()
  136.         {
  137.             return array (
  138.                 'file_name'         => $this->file_name,
  139.                 'file_type'         => $this->file_type,
  140.                 'file_path'         => $this->upload_path,
  141.                 'full_path'         => $this->upload_path.$this->file_name,
  142.                 'raw_name'          => str_replace($this->file_ext, '', $this->file_name),
  143.                 'orig_name'         => $this->orig_name,
  144.                 'client_name'       => $this->client_name,
  145.                 'file_ext'              => $this->file_ext,
  146.                 'file_size'             => $this->file_size,
  147.                 'is_image'          => $this->is_image(),
  148.                 'image_width'       => $this->image_width,
  149.                 'image_height'      => $this->image_height,
  150.                 'image_type'        => $this->image_type,
  151.                 'image_size_str'    => $this->image_size_str,
  152.                 'ip_address'         => $this->ip_address
  153.             );
  154.         }
  155.  
  156.  
  157.         // --------------------------------------------------------------------
  158.         /*
  159.             Function: do_upload_array()
  160.  
  161.             Description:
  162.                 Uploads the files.
  163.  
  164.             Parameters:
  165.                 $field - Refers to the name of the field element
  166.                 $key - Refers to the array of the field element
  167.  
  168.             Returns:
  169.                 Object -> status
  170.                     TRUE - If all the files have been uploaded successfully
  171.                     FALSE - If there are errors
  172.                 Object -> file_info
  173.                         An array containing the file information as returned from this->data
  174.        
  175.             See Also:
  176.                 <upload_files>
  177.                 <validate_upload_path>
  178.         */
  179.         public function do_upload_array($field, $key, $index='')
  180.         {
  181.             $response = new stdClass();
  182.             $response->status = FALSE;
  183.            
  184.             // Is $_FILES[$field] set? If not, no reason to continue.
  185.             if (!isset($_FILES[$field]))
  186.             {
  187.                 $this->set_error('upload_no_file_selected');
  188.                 return $response;
  189.             }
  190.  
  191.             // Is the upload path valid?
  192.             if ( ! $this->validate_upload_path())
  193.             {
  194.                 // errors will already be set by validate_upload_path() so just return $response
  195.                 return $response;
  196.             }
  197.  
  198.             if($index !== ''){
  199.                 // Was the file able to be uploaded? If not, determine the reason why.
  200.                 if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$index][$key]))
  201.                 {
  202.                     $error = ( ! isset($_FILES[$field]['error'][$index][$key])) ? 4 : $_FILES[$field]['error'][$index][$key];
  203.  
  204.                     switch($error)
  205.                     {
  206.                         case 1: // UPLOAD_ERR_INI_SIZE
  207.                             $this->set_error('upload_file_exceeds_limit');
  208.                             break;
  209.                         case 2: // UPLOAD_ERR_FORM_SIZE
  210.                             $this->set_error('upload_file_exceeds_form_limit');
  211.                             break;
  212.                         case 3: // UPLOAD_ERR_PARTIAL
  213.                             $this->set_error('upload_file_partial');
  214.                             break;
  215.                         case 4: // UPLOAD_ERR_NO_FILE
  216.                             $this->set_error('upload_no_file_selected');
  217.                             break;
  218.                         case 6: // UPLOAD_ERR_NO_TMP_DIR
  219.                             $this->set_error('upload_no_temp_directory');
  220.                             break;
  221.                         case 7: // UPLOAD_ERR_CANT_WRITE
  222.                             $this->set_error('upload_unable_to_write_file');
  223.                             break;
  224.                         case 8: // UPLOAD_ERR_EXTENSION
  225.                             $this->set_error('upload_stopped_by_extension');
  226.                             break;
  227.                         default :   $this->set_error('upload_no_file_selected');
  228.                         break;
  229.                     }
  230.  
  231.                     return $response;
  232.                 }
  233.  
  234.  
  235.                 // Set the uploaded data as class variables
  236.                 $this->file_temp = $_FILES[$field]['tmp_name'][$index][$key];
  237.                 $this->file_size = $_FILES[$field]['size'][$index][$key];
  238.                 $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$index][$key]);
  239.                 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
  240.                 $this->file_name = $this->_prep_filename($_FILES[$field]['name'][$index][$key]);
  241.                 $this->file_ext  = $this->get_extension($this->file_name);
  242.                 $this->client_name = $this->file_name;
  243.             }else{
  244.                 // Was the file able to be uploaded? If not, determine the reason why.
  245.                 if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$key]))
  246.                 {
  247.                     $error = ( ! isset($_FILES[$field]['error'][$key])) ? 4 : $_FILES[$field]['error'][$key];
  248.  
  249.                     switch($error)
  250.                     {
  251.                         case 1: // UPLOAD_ERR_INI_SIZE
  252.                             $this->set_error('upload_file_exceeds_limit');
  253.                             break;
  254.                         case 2: // UPLOAD_ERR_FORM_SIZE
  255.                             $this->set_error('upload_file_exceeds_form_limit');
  256.                             break;
  257.                         case 3: // UPLOAD_ERR_PARTIAL
  258.                             $this->set_error('upload_file_partial');
  259.                             break;
  260.                         case 4: // UPLOAD_ERR_NO_FILE
  261.                             $this->set_error('upload_no_file_selected');
  262.                             break;
  263.                         case 6: // UPLOAD_ERR_NO_TMP_DIR
  264.                             $this->set_error('upload_no_temp_directory');
  265.                             break;
  266.                         case 7: // UPLOAD_ERR_CANT_WRITE
  267.                             $this->set_error('upload_unable_to_write_file');
  268.                             break;
  269.                         case 8: // UPLOAD_ERR_EXTENSION
  270.                             $this->set_error('upload_stopped_by_extension');
  271.                             break;
  272.                         default :   $this->set_error('upload_no_file_selected');
  273.                         break;
  274.                     }
  275.  
  276.                     return $response;
  277.                 }
  278.  
  279.  
  280.                 // Set the uploaded data as class variables
  281.                 $this->file_temp = $_FILES[$field]['tmp_name'][$key];
  282.                 $this->file_size = $_FILES[$field]['size'][$key];
  283.                 $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$key]);
  284.                 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
  285.                 $this->file_name = $this->_prep_filename($_FILES[$field]['name'][$key]);
  286.                 $this->file_ext  = $this->get_extension($this->file_name);
  287.                 $this->client_name = $this->file_name;
  288.             }
  289.  
  290.             // Is the file type allowed to be uploaded?
  291.             if ( ! $this->is_allowed_filetype())
  292.             {
  293.                 $this->set_error('upload_invalid_filetype');
  294.                 return $response;
  295.             }
  296.  
  297.             // if we're overriding, let's now make sure the new name and type is allowed
  298.             if ($this->_file_name_override != '')
  299.             {
  300.                 $this->file_name = $this->_prep_filename($this->_file_name_override);
  301.  
  302.                 // If no extension was provided in the file_name config item, use the uploaded one
  303.                 if (strpos($this->_file_name_override, '.') === FALSE)
  304.                 {
  305.                     $this->file_name .= $this->file_ext;
  306.                 }
  307.  
  308.                 // An extension was provided, lets have it!
  309.                 else
  310.                 {
  311.                     $this->file_ext  = $this->get_extension($this->_file_name_override);
  312.                 }
  313.  
  314.                 if ( ! $this->is_allowed_filetype(TRUE))
  315.                 {
  316.                     $this->set_error('upload_invalid_filetype');
  317.                     return $response;
  318.                 }
  319.             }
  320.  
  321.             // Convert the file size to kilobytes
  322.             if ($this->file_size > 0)
  323.             {
  324.                 $this->file_size = round($this->file_size/1024, 2);
  325.             }
  326.  
  327.             // Is the file size within the allowed maximum?
  328.             if ( ! $this->is_allowed_filesize())
  329.             {
  330.                 $this->set_error('upload_invalid_filesize');
  331.                 return $response;
  332.             }
  333.  
  334.             // Are the image dimensions within the allowed size?
  335.             // Note: This can fail if the server has an open_basdir restriction.
  336.             if ( ! $this->is_allowed_dimensions())
  337.             {
  338.                 $this->set_error('upload_invalid_dimensions');
  339.                 return $response;
  340.             }
  341.  
  342.             // Sanitize the file name for security
  343.             $this->file_name = $this->clean_file_name($this->file_name);
  344.  
  345.             // Truncate the file name if it's too long
  346.             if ($this->max_filename > 0)
  347.             {
  348.                 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
  349.             }
  350.  
  351.             // Remove white spaces in the name
  352.             if ($this->remove_spaces == TRUE)
  353.             {
  354.                 $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
  355.             }
  356.  
  357.             /*
  358.              * Validate the file name
  359.              * This function appends an number onto the end of
  360.              * the file if one with the same name already exists.
  361.              * If it returns false there was a problem.
  362.              */
  363.             $this->orig_name = $this->file_name;
  364.  
  365.             if ($this->overwrite == FALSE)
  366.             {
  367.                 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
  368.  
  369.                 if ($this->file_name === FALSE)
  370.                 {
  371.                     return $response;
  372.                 }
  373.             }
  374.  
  375.             /*
  376.              * Run the file through the XSS hacking filter
  377.              * This helps prevent malicious code from being
  378.              * embedded within a file.  Scripts can easily
  379.              * be disguised as images or other file types.
  380.              */
  381.             if ($this->xss_clean)
  382.             {
  383.                 if ($this->do_xss_clean() === FALSE)
  384.                 {
  385.                     $this->set_error('upload_unable_to_write_file');
  386.                     return $response;
  387.                 }
  388.             }
  389.  
  390.             /*
  391.              * Move the file to the final destination
  392.              * To deal with different server configurations
  393.              * we'll attempt to use copy() first.  If that fails
  394.              * we'll use move_uploaded_file().  One of the two should
  395.              * reliably work in most environments
  396.              */
  397.             if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
  398.             {
  399.                 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
  400.                 {
  401.                     $this->set_error('upload_destination_error');
  402.                     return $response;
  403.                 }
  404.             }
  405.  
  406.             /*
  407.              * Set the finalized image dimensions
  408.              * This sets the image width/height (assuming the
  409.              * file was an image).  We use this information
  410.              * in the "data" function.
  411.              */
  412.             $this->set_image_properties($this->upload_path.$this->file_name);
  413.  
  414.             $response->status = TRUE;
  415.             $response->file_info = $this->data();
  416.             return $response;
  417.         }
  418.  
  419.         /**
  420.          * Validate Upload Path
  421.          *
  422.          * Verifies that it is a valid upload path with proper permissions.
  423.          *
  424.          * ADDED: Creates directory if the directory doesn't exist
  425.          *
  426.          * @return  bool
  427.          */
  428.         public function validate_upload_path()
  429.         {
  430.             if ($this->upload_path == '')
  431.             {
  432.                 $this->set_error('upload_no_filepath');
  433.                 return FALSE;
  434.             }
  435.  
  436.             if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
  437.             {
  438.                 $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
  439.             }
  440.  
  441.             if ( ! @is_dir($this->upload_path))
  442.             {
  443.                 if(!mkdir($this->upload_path, 0777, true))
  444.                 {
  445.                     $this->set_error('upload_no_filepath');
  446.                     return FALSE;
  447.                 }
  448.             }
  449.  
  450.             if ( ! is_really_writable($this->upload_path))
  451.             {
  452.                 $this->set_error('upload_not_writable');
  453.                 return FALSE;
  454.             }
  455.  
  456.             $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/",  $this->upload_path);
  457.             return TRUE;
  458.         }
  459.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement