How To Upload Multiple Files Using Codeigniter ?

This is simply an extension to the existing Codeigniter's Upload flat to accept wages of multiple uploads inwards i shape field.

Installation Copy MY_Upload.php to your application/libraries directory. If y'all are using a prefix other than MY_ for your classes and/or choice file paths, conform accordingly.

Usage Load the Upload flat equally usual, inwards your controller telephone band the component do_multi_upload(). If y'all are uploading a unmarried file, it'll default to the regular do_upload() method. In the form, laid plain names to array (e.g. name="userfile[]")

MY_Upload.php
 <?php if ( ! defined('BASEPATH')) exit('No straight script access allowed');  /**  * Exenstion File Uploading Class  */    flat MY_Upload extends CI_Upload {    world component do_multi_upload( $field = 'userfile', $return_info = TRUE, $filenames = NULL ){    // Is $_FILES[$field] set? If not, no argue to continue.   if ( ! isset($_FILES[$field]))   {       $this->set_error('upload_no_file_selected');    render FALSE;      }      //If non every file filled was used, clear the empties      foreach( $_FILES[$field]['name'] equally $k => $n )   {     if( empty( $n ) )    {         foreach( $_FILES[$field] equally $kk => $f )     {          unset( $_FILES[$field][$kk][$k] );          }            }      }      // Is the upload path valid?   if ( ! $this->validate_upload_path($field) )   {     // errors volition already live laid past times validate_upload_path() in addition to then simply render FALSE    render FALSE;   }    //Multiple file upload   if( is_array( $_FILES[$field] ) )   {     //$count = count($_FILES[$field]['name']); //Number of files to procedure        foreach( $_FILES[$field]['name'] equally $k => $file )    {         // Was the file able to live uploaded? If not, decide the argue why.     if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$k] ) )     {           $error = ( ! isset($_FILES[$field]['error'][$k])) ? iv : $_FILES[$field]['error'][$k];            switch($error)      {       instance 1: // UPLOAD_ERR_INI_SIZE        $this->set_error('upload_file_exceeds_limit');        break;       instance 2: // UPLOAD_ERR_FORM_SIZE        $this->set_error('upload_file_exceeds_form_limit');        break;       instance 3: // UPLOAD_ERR_PARTIAL        $this->set_error('upload_file_partial');        break;       instance 4: // UPLOAD_ERR_NO_FILE        $this->set_error('upload_no_file_selected');        break;       instance 6: // UPLOAD_ERR_NO_TMP_DIR        $this->set_error('upload_no_temp_directory');        break;       instance 7: // UPLOAD_ERR_CANT_WRITE        $this->set_error('upload_unable_to_write_file');        break;       instance 8: // UPLOAD_ERR_EXTENSION        $this->set_error('upload_stopped_by_extension');        break;       default :   $this->set_error('upload_no_file_selected');        break;      }         render FALSE;     }              // Set the uploaded information equally flat variables     $this->file_temp = $_FILES[$field]['tmp_name'][$k];     $this->file_size = $_FILES[$field]['size'][$k];     $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$k]);     $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));          if(empty($filenames))     {      $this->file_name = $this->_prep_filename($_FILES[$field]['name'][$k]);          }     else     {      $this->file_name = $this->_prep_filename($filenames[$k]);     }          $this->file_ext  = $this->get_extension($this->file_name);     $this->client_name = $this->file_name;          // Is the file type allowed to live uploaded?     if ( ! $this->is_allowed_filetype())     {      $this->set_error('upload_invalid_filetype');      render FALSE;     }          // if we're overriding, let's directly brand certain the novel cite in addition to type is allowed     if ($this->_file_name_override != '')     {      $this->file_name = $this->_prep_filename($this->_file_name_override);         // If no extension was provided inwards the file_name config item, work the uploaded i      if (strpos($this->_file_name_override, '.') === FALSE)      {       $this->file_name .= $this->file_ext;      }         // An extension was provided, lets get got it!      else      {       $this->file_ext  = $this->get_extension($this->_file_name_override);      }         if ( ! $this->is_allowed_filetype(TRUE))      {       $this->set_error('upload_invalid_filetype');       render 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 inside the allowed maximum?     if ( ! $this->is_allowed_filesize())     {      $this->set_error('upload_invalid_filesize');      render FALSE;     }        // Are the ikon dimensions inside the allowed size?     // Note: This tin terminate neglect if the server has an open_basdir restriction.     if ( ! $this->is_allowed_dimensions())     {      $this->set_error('upload_invalid_dimensions');      render FALSE;     }       // Sanitize the file cite for safety     $this->file_name = $this->clean_file_name($this->file_name);        // Truncate the file cite if it's equally good long     if ($this->max_filename > 0)     {      $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);     }        // Remove white spaces inwards the cite     if ($this->remove_spaces == TRUE)     {      $this->file_name = preg_replace("/\s+/", "_", $this->file_name);     }        /*      * Validate the file cite      * This component appends an publish onto the halt of      * the file if i alongside the same cite already exists.      * If it returns imitation in that location 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)      {       render FALSE;      }     }        /*      * Run the file through the XSS hacking filter      * This helps forbid malicious code from beingness      * embedded inside a file.  Scripts tin terminate easily      * live disguised equally images or other file types.      */     if ($this->xss_clean)     {      if ($this->do_xss_clean() === FALSE)      {       $this->set_error('upload_unable_to_write_file');       render FALSE;      }     }        /*      * Move the file to the lastly goal      * To bargain alongside dissimilar server configurations      * we'll endeavor to work copy() first.  If that fails      * we'll work move_uploaded_file().  One of the ii should      * reliably piece of work inwards close 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');       render FALSE;      }     }        /*      * Set the finalized ikon dimensions      * This sets the ikon width/height (assuming the      * file was an image).  We work this information      * inwards the "data" function.      */     $this->set_image_properties($this->upload_path.$this->file_name);             if( $return_info === TRUE )     {           $return_value[$k] = $this->data();         }     else     {          $return_value = TRUE;         }        }        render $return_value;     }   else //Single file upload, rely on native CI upload flat   {      $upload = self::do_upload();       render $upload;     }   }  }  ?> 
Source: https://github.com/nicdev/CodeIgniter-Multiple-File-Upload
Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?