Guest User

Untitled

a guest
Sep 19th, 2011
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.97 KB | None | 0 0
  1.     <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.    
  3.     class Multi_upload  {
  4.         function Multi_upload () {
  5.     //      $CI =& get_instance();
  6.         }
  7.        
  8.         /**
  9.          * Perform multiple file uploads
  10.          * Based upon JQuery Multiple Upload Class
  11.          * see http://www.fyneworks.com/jquery/multiple-file-upload/
  12.          */
  13.         public function go_upload($field = 'userfile', $i = 0) {
  14.             $CI =& get_instance();
  15.             // Is $_FILES[$field] set? If not, no reason to continue.
  16.             if ( ! isset($_FILES[$field]['name'][0]))
  17.             {
  18.                 $CI->upload->set_error('upload_no_file_selected');
  19.                 return FALSE;
  20.             } else
  21.             {
  22.                 $num_files = count($_FILES[$field]['name']) -1;
  23.                 $file_list = array();
  24.                 $error_hold = array();
  25.                 $error_upload = FALSE;
  26.             }
  27.            
  28.             // Is the upload path valid?
  29.             if ( ! $CI->upload->validate_upload_path())
  30.             {
  31.                 // errors will already be set by validate_upload_path() so just return FALSE
  32.                 return FALSE;
  33.             }
  34.            
  35.             for ($i=0; $i < $num_files; $i++) {
  36.                
  37.     //            $fname = $_FILES[$field]['name'][$i];
  38.     //            echo "$fname\n\n<br><br>\n\n";
  39.    
  40.                 $error_hold[$i] = FALSE;
  41.                            
  42.                 // Was the file able to be uploaded? If not, determine the reason why.
  43.                 if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$i]))
  44.                 {
  45.                     $error = ( ! isset($_FILES[$field]['error'][$i])) ? 4 : $_FILES[$field]['error'][$i];
  46.    
  47.                     switch($error)
  48.                     {
  49.                         case 1:  // UPLOAD_ERR_INI_SIZE
  50.                             $error_hold[$i] = 'upload_file_exceeds_limit';
  51.                             break;
  52.                         case 2: // UPLOAD_ERR_FORM_SIZE
  53.                             $error_hold[$i] = 'upload_file_exceeds_form_limit';
  54.                             break;
  55.                         case 3: // UPLOAD_ERR_PARTIAL
  56.                            $error_hold[$i] = 'upload_file_partial';
  57.                             break;
  58.                         case 4: // UPLOAD_ERR_NO_FILE
  59.                            $error_hold[$i] = 'upload_no_file_selected';
  60.                             break;
  61.                         case 6: // UPLOAD_ERR_NO_TMP_DIR
  62.                             $error_hold[$i] = 'upload_no_temp_directory';
  63.                             break;
  64.                         case 7: // UPLOAD_ERR_CANT_WRITE
  65.                             $error_hold[$i] = 'upload_unable_to_write_file';
  66.                             break;
  67.                         case 8: // UPLOAD_ERR_EXTENSION
  68.                             $error_hold[$i] = 'upload_stopped_by_extension';
  69.                             break;
  70.                         default :
  71.                             $error_hold[$i] = 'upload_no_file_selected';
  72.                             break;
  73.                     }
  74.    
  75.                     return FALSE;
  76.                 }
  77.    
  78.                 // Set the uploaded data as class variables
  79.                 $CI->upload->file_temp = $_FILES[$field]['tmp_name'][$i];
  80.             $CI->upload->file_name = $_FILES[$field]['name'][$i];
  81.                 $CI->upload->file_size = $_FILES[$field]['size'][$i];        
  82.                 $CI->upload->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]);
  83.                 $CI->upload->file_type = strtolower($CI->upload->file_type);
  84.                 $CI->upload->file_ext  = $CI->upload->get_extension($_FILES[$field]['name'][$i]);
  85.                
  86.                 // Convert the file size to kilobytes
  87.                 if ($CI->upload->file_size > 0)
  88.                 {
  89.                     $CI->upload->file_size = round($CI->upload->file_size/1024, 2);
  90.                 }
  91.    
  92.                 // Is the file type allowed to be uploaded?
  93.                 if ( ! $CI->upload->is_allowed_filetype())
  94.                 {
  95.                     $error_hold[$i] = 'upload_invalid_filetype';
  96.                 }
  97.    
  98.                 // Is the file size within the allowed maximum?
  99.                 if ( ! $CI->upload->is_allowed_filesize())
  100.                 {
  101.                     $error_hold[$i] = 'upload_invalid_filesize';
  102.                 }
  103.    
  104.                 // Are the image dimensions within the allowed size?
  105.                 // Note: This can fail if the server has an open_basdir restriction.
  106.                 if ( ! $CI->upload->is_allowed_dimensions())
  107.                 {
  108.                     $error_hold[$i] = 'upload_invalid_dimensions';
  109.                 }
  110.    
  111.                 // Sanitize the file name for security
  112.                 $CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name);
  113.    
  114.                 // Remove white spaces in the name
  115.                 if ($CI->upload->remove_spaces == TRUE)
  116.                 {
  117.                     $CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name);
  118.                 }
  119.    
  120.                 /*
  121.                  * Validate the file name
  122.                  * This function appends an number onto the end of
  123.                  * the file if one with the same name already exists.
  124.                  * If it returns false there was a problem.
  125.                  */
  126.                 $CI->upload->orig_name = $CI->upload->file_name;
  127.    
  128.                 if ($CI->upload->overwrite == FALSE)
  129.                 {
  130.                     $CI->upload->file_name = $CI->upload->set_filename($CI->upload->upload_path, $CI->upload->file_name);
  131.                    
  132.                     if ($CI->upload->file_name === FALSE)
  133.                     {
  134.                         $error_hold[$i] = TRUE;
  135.                     }
  136.                 }
  137.    
  138.                 /*
  139.                  * Move the file to the final destination
  140.                  * To deal with different server configurations
  141.                  * we'll attempt to use copy() first.  If that fails
  142.                  * we'll use move_uploaded_file().  One of the two should
  143.                  * reliably work in most environments
  144.                  */
  145.                 if ( ! @copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
  146.                 {
  147.                     if ( ! @move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
  148.                     {
  149.                          $error_hold[$i] = 'upload_destination_error';
  150.                     }
  151.                 }
  152.                
  153.                 /*
  154.                  * Run the file through the XSS hacking filter
  155.                  * This helps prevent malicious code from being
  156.                  * embedded within a file.  Scripts can easily
  157.                  * be disguised as images or other file types.
  158.                  */
  159.                 if ($CI->upload->xss_clean == TRUE)
  160.                 {
  161.                     $CI->upload->do_xss_clean();
  162.                 }
  163.                
  164.                 if ($error_hold[$i]) {
  165.                     $error_upload = TRUE;
  166.                    
  167.     //                echo $error_hold[$i];
  168.                 } else {
  169.                     if ($imageVar = $this->multiple_image_properties($CI->upload->upload_path.$CI->upload->file_name)) {
  170.    
  171.                         $file_list[] = array(
  172.                                 'name' => $CI->upload->file_name,
  173.                                 'file' => $CI->upload->upload_path.$CI->upload->file_name,
  174.                                 'size' => $CI->upload->file_size,
  175.                                 'ext' => $CI->upload->file_ext,
  176.                                 'image_type' => $imageVar->image_type,
  177.                                 'height' => $imageVar->height,
  178.                                 'width' => $imageVar->width
  179.                                 );
  180.                     } else {
  181.                         $file_list[] = array(
  182.                                 'name' => $CI->upload->file_name,
  183.                                 'file' => $CI->upload->upload_path.$CI->upload->file_name,
  184.                                 'size' => $CI->upload->file_size,
  185.                                 'type' => $CI->upload->file_type,
  186.                                 'ext' => $CI->upload->file_ext,
  187.                                 );
  188.                     }
  189.                 }
  190.    
  191.     // For debugging
  192.     /*            
  193.                 if (strlen($error_hold[$i]) > 1) {
  194.                         print_r($error_hold);
  195.                 }
  196.     */            
  197.             } // end for loop
  198.    
  199.     // Add error display for individual files        
  200.             if ($error_upload) {
  201.                 $this->set_error($error_hold);
  202.                 return FALSE;
  203.             } else {
  204.                 return $file_list;
  205.             }    
  206.         }
  207.        
  208.         // --------------------------------------------------------------------
  209.    
  210.         /**
  211.          * Set Image Properties
  212.          *
  213.          * Uses GD to determine the width/height/type of image
  214.          *
  215.          * @access    public
  216.          * @param    string
  217.          * @return    void
  218.          */    
  219.         function multiple_image_properties($path = '')
  220.         {
  221.             $CI =& get_instance();
  222.             if ( ! $CI->upload->is_image())
  223.             {
  224.                 return false;
  225.             }
  226.    
  227.             if (function_exists('getimagesize'))
  228.             {
  229.                 if (FALSE !== ($D = @getimagesize($path)))
  230.                 {    
  231.                     $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
  232.    
  233.                     $image->width       = $D['0'];
  234.                     $image->height      = $D['1'];
  235.                     $image->image_type        = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
  236.                    
  237.                     return $image;
  238.                 }
  239.             }
  240.         }
  241.        
  242.         // --------------------------------------------------------------------
  243.    
  244.         /**
  245.          * Set an error message
  246.          *
  247.          * @access    public
  248.          * @param    string
  249.          * @return    void
  250.          */    
  251.         function set_error($msg)
  252.         {
  253.             $CI =& get_instance();    
  254.             $CI->lang->load('upload');
  255.            
  256.             if (is_array($msg))
  257.             {
  258.                 foreach ($msg as $val)
  259.                 {
  260.                     $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);                
  261.                     $this->error_msg[] = $msg;
  262.                     log_message('error', $msg);
  263.                 }        
  264.             }
  265.             else
  266.             {
  267.                 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
  268.                 $this->error_msg[] = $msg;
  269.                 log_message('error', $msg);
  270.             }
  271.         }
  272.        
  273.         // --------------------------------------------------------------------
  274.     }
  275.     ?>
Advertisement
Add Comment
Please, Sign In to add comment