• 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.

#12
[eluser]Romyblack[/eluser]
[quote author="Nihilistik" date="1332955885"]Hi there!

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

[/quote]

Hi, Nihilistik!

You have this in your view file
Code:
&lt;form method="POST" action="" enctype="multipart/form-data"&gt;
    &lt;input type="files[]" name="file_1" size="20" multiple="multiple"/&gt;  
    &lt;input type="submit" name="test" value="TEST" /&gt;
&lt;/form&gt;

But it should be like this according to the w3s documentation
Code:
&lt;form method="POST" action="" enctype="multipart/form-data"&gt;
    &lt;input type="file" name="files[]" size="20" multiple="multiple"/&gt;  
    &lt;input type="submit" name="test" value="TEST" /&gt;
&lt;/form&gt;

I really appreciate that this library its useful to other people, please, be free to make any modifications according to your needs,.

#13
[eluser]karlis_i[/eluser]
Modified this great library to enable optional file uploads ( no more "You did not select a file to upload." messages )-
What you need to do is wrap upload process in "if" block at line 115:

replace
Code:
#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()));
    }
}

with
Code:
#Here we do the upload process
foreach ($uploaded_files as $file => $value) {
    if( $value['size'] > 0 ){
        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()));
        }
    }
}

#14
[eluser]Romyblack[/eluser]
[quote author="karlis_i" date="1333018873"]Modified this great library to enable optional file uploads ( no more "You did not select a file to upload." messages )-
What you need to do is wrap upload process in "if" block at line 115:

replace
Code:
#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()));
    }
}

with
Code:
#Here we do the upload process
foreach ($uploaded_files as $file => $value) {
    if( $value['size'] > 0 ){
        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()));
        }
    }
}
[/quote]

Thanks for your contribution karlis_i

#15
[eluser]HunterJoe1977[/eluser]
[quote author="karlis_i" date="1333018873"]Modified this great library to enable optional file uploads ( no more "You did not select a file to upload." messages )-
What you need to do is wrap upload process in "if" block at line 115:

replace
Code:
#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()));
    }
}

with
Code:
#Here we do the upload process
foreach ($uploaded_files as $file => $value) {
    if( $value['size'] > 0 ){
        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()));
        }
    }
}
[/quote]

This is perfect! Exactly what I needed. Thank you.

#16
[eluser]somenet[/eluser]
Please Help me to create thumbnail also.

#17
[eluser]Romyblack[/eluser]
[quote author="somenet" date="1338711276"]Please Help me to create thumbnail also.[/quote]

Hi, right know i'm at work but i've already worked with thumbnails creation, if you want or decide send me a private message with your email so i can send you an example on how to create thumbnails.

#18
[eluser]Unknown[/eluser]
Hi romyblack!
thanks for sharing this, i am also new here and trying to learn something everyday.

<a href="http://getfacebookfanzs.net/">Get Facebook Fanzs</a>

#19
[eluser]somenet[/eluser]
hi my email id is [email protected]

#20
[eluser]Romyblack[/eluser]
HI,

I UPDATED THE LIBRARY SO IT NOW ACCEPTS MULTIDIMENSIOAL AND BIDIMENSIONAL
FILES' ARRAY Smile

NOW WITH THE UPDATED LIBRARY YOU CAN DO THIS.

Code:
&lt;form method="POST" action="" enctype="multipart/form-data"&gt;
&lt;input type="file" name="file[]" size="20" /&gt;
&lt;input type="file" name="file[]" size="20" /&gt;
&lt;input type="file" name="file1" size="20" /&gt;
&lt;input type="file" name="file2" size="20" /&gt;
&lt;input type="submit" name="test" value="TEST" /&gt;
&lt;/form&gt;
YOU CAN USE EVEN HYBRIDS FILE INPUTS IF YOU DESIRE.

Here's the link http://ellislab.com/forums/viewthread/207911/
Hope you enjoy, special thanks to every person who's using this library.

MY_Paypal Library coming soon Smile
This library will be the easiest to implement and everyone will be capable to handle payment process in their projects.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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