• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple file upload library

#11
[eluser]Unknown[/eluser]
Hi there!

I'm trying to learn CodeIgniter, and there is no better way than find something that you need and there isn't any solution that match to your needs.

So, in this case i need to upload files through a multiple input file, what, as you know, require an "array name" like
Code:
name="files[]"
and a multiple attribute
Code:
multiple="multiple"

I don't know if there is any issue about this attribute, let me know if it is. My solution for this way to upload images was based on Romyblack solution... thanks for it!

I'm gonna post the modified code only:

My view form (Edited for correction, thx Romyblack)
Code:
<form method="POST" action="" enctype="multipart/form-data">
    <input type="file" name="files[]" size="20" multiple="multiple"/>  
    <input type="submit" name="test" value="TEST" />
</form>

Modified library
Code:
public function up($protect = FALSE){

  /*
  * Declare uploaded_info and uploaded_files
  * when i'm sure $_FILES has some data
  */
  if($this->upload_path[strlen($this->upload_path)-1] != '/')
   $this->upload_path .= '/';

  
  if(isset($_FILES)){
  
   #Here we check if the path exists if not then create
   if(!file_exists($this->upload_path)){
    @mkdir($this->upload_path,0700,TRUE);
   }
    $uploaded_info  = FALSE;
    /*
    * The structure of $_FILES changes a lot with the array name on the input file,
    * then i'm gonna modify $_FILES to make it think the data comes from several
    * input file instead of one "arrayfied" input.
    *
    * The several ways to upload files are controled with this if...else structure
    */
    if(count($_FILES) == 1)
    {
        $main_key = key($_FILES);
        if(is_array($_FILES[$main_key]['name']))
        {
            
            foreach($_FILES[$main_key] as $key => $value)
            {                
                
                for($y = 0; $y < count($value); $y++)
                {
                    
                    $_FILES[$main_key .'-'. $y][$key] = $value[$y];
                        
                }
                
                
            }
            
            unset($_FILES[$main_key]);
            
            $uploaded_files  = $_FILES;
        }
        else
        {
            $uploaded_files  = $_FILES;    
        }
        
    }
    else
    {
        $uploaded_files  = $_FILES;    
    }
    
   #Here we create the index file in each path's directory
   if($protect){
    $folder = '';
    foreach(explode('/',$this->upload_path)  as $f){
    
     $folder .= $f.'/';
     $text = "&lt;?php echo 'Directory access is forbidden.'; ?&gt;";
    
     if(!file_exists($folder.'index.php')){
      $index = $folder.'index.php';
      $Handle = fopen($index, 'w');
      fwrite($Handle, trim($text));
      fclose($Handle);
     }
    }  
   }

   #Here we do the upload process
    
   foreach($uploaded_files as $file => $value){
    if (!$this->do_upload($file))
    {
     $uploaded_info['error'][]  =  array_merge($this->data(),
              array('error_msg' => $this->display_errors()));
    
    }
    else
    {
     $uploaded_info['success'][] =  array_merge($this->data(),
              array('error_msg' => $this->display_errors()));
    }
   }  
  }
  
  #Then return what happened with the files
  return $uploaded_info;
}

It's working in my project, i hope this will be useful. The usage it's the same that
Romyblack explain at the begining of his library.

Let me know if there is any kind of issue or style violation.


Messages In This Thread
Multiple file upload library - by El Forum - 01-06-2012, 05:36 AM
Multiple file upload library - by El Forum - 01-19-2012, 03:56 PM
Multiple file upload library - by El Forum - 01-20-2012, 11:26 AM
Multiple file upload library - by El Forum - 01-20-2012, 03:57 PM
Multiple file upload library - by El Forum - 01-21-2012, 04:18 PM
Multiple file upload library - by El Forum - 01-27-2012, 07:45 PM
Multiple file upload library - by El Forum - 02-06-2012, 08:42 AM
Multiple file upload library - by El Forum - 02-06-2012, 08:59 AM
Multiple file upload library - by El Forum - 02-06-2012, 02:22 PM
Multiple file upload library - by El Forum - 02-07-2012, 06:20 AM
Multiple file upload library - by El Forum - 03-28-2012, 10:31 AM
Multiple file upload library - by El Forum - 03-28-2012, 08:35 PM
Multiple file upload library - by El Forum - 03-29-2012, 04:01 AM
Multiple file upload library - by El Forum - 05-18-2012, 12:15 PM
Multiple file upload library - by El Forum - 05-23-2012, 08:32 AM
Multiple file upload library - by El Forum - 06-03-2012, 01:14 AM
Multiple file upload library - by El Forum - 06-08-2012, 01:01 PM
Multiple file upload library - by El Forum - 06-08-2012, 09:13 PM
Multiple file upload library - by El Forum - 06-09-2012, 04:05 AM
Multiple file upload library - by El Forum - 06-09-2012, 05:28 PM
Multiple file upload library - by El Forum - 06-14-2012, 03:41 PM
Multiple file upload library - by El Forum - 06-14-2012, 07:23 PM
Multiple file upload library - by El Forum - 06-16-2012, 02:51 AM
Multiple file upload library - by El Forum - 08-06-2012, 05:14 AM
Multiple file upload library - by El Forum - 08-06-2012, 05:23 AM
Multiple file upload library - by El Forum - 08-06-2012, 06:30 AM
Multiple file upload library - by El Forum - 08-08-2012, 06:41 AM
Multiple file upload library - by El Forum - 08-08-2012, 06:45 AM
Multiple file upload library - by El Forum - 08-18-2012, 06:45 AM
Multiple file upload library - by El Forum - 08-21-2012, 02:14 PM
Multiple file upload library - by El Forum - 08-21-2012, 11:25 PM
Multiple file upload library - by El Forum - 08-22-2012, 02:45 AM
Multiple file upload library - by El Forum - 10-04-2012, 11:25 PM
Multiple file upload library - by El Forum - 10-13-2012, 10:48 PM
Multiple file upload library - by El Forum - 10-20-2012, 08:33 PM
Multiple file upload library - by El Forum - 10-20-2012, 08:35 PM
Multiple file upload library - by El Forum - 10-20-2012, 09:32 PM
Multiple file upload library - by El Forum - 10-20-2012, 09:34 PM
Multiple file upload library - by El Forum - 10-21-2012, 08:45 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.