CodeIgniter Forums
Avoid re-upload on multiple uploads. (If error appear) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Avoid re-upload on multiple uploads. (If error appear) (/showthread.php?tid=13004)



Avoid re-upload on multiple uploads. (If error appear) - El Forum - 11-07-2008

[eluser]Ignacio[/eluser]
Hello everybody, I'm using this code from xwero to do the multiple uploads, multiple folders and multiple configs, works great!:
Code:
// MY_Upload.php
class MY_Upload extends CI_Upload
{
   function multi_upload($configs,$files)
   {
    
    if(count($configs) != count($files))
    {
       return 'array_count_wrong';
    }
    
    $errors = $successes = array();

    for($i=0, $j = count($files);$i<$j;$i++)
    {
       $this->initialize($configs[$i]);

       if( ! $this->do_upload($files[$i]))
       {
           $errors[$files[$i]] = $this->display_errors();
       }
       else
       {
           $successes[$files[$i]] = $this->data();
       }
    }

    return array($errors, $successes);
  }
}
// controller
$this->load->library('upload');

$config[0]['upload_path'] = './_uploads/images/';
            $config[0]['allowed_types'] = 'gif|jpg|png';
            $config[0]['max_size']    = '1000';
            $config[0]['max_width']  = '50';
            $config[0]['max_height']  = '50';
            $config[1]['upload_path'] = './_uploads/flv/';
            $config[1]['allowed_types'] = 'flv';
            $config[1]['max_size']    = '30000';

            // files
            $files[0] = 'userfile_0';
            $files[1] = 'userfile_1';

            // upload
            $messages = $this->upload->multi_upload($config,$files);

Now, the problem (nobody ask this before). Check the controller on the example, I'm using two fields, so, two files to upload, lets say file1 and file2.

If file1 do the upload fine and file2 not, what happen? Retrieve an error for file2, that's excellent, but what happen with file1? you need to re-upload again! I don't want that! Any idea of how can I improve a solution for this?

Thanks!


Avoid re-upload on multiple uploads. (If error appear) - El Forum - 11-07-2008

[eluser]hostcord[/eluser]
What if this...

Code:
if( ! $this->do_upload($files[$i]))

was..

Code:
if( ! is_file($configs[$i]['upload_path'][$files[$i]]) && ! $this->do_upload($files[$i]))

It should fail before it gets to the do_upload method but I haven't tested it.


Avoid re-upload on multiple uploads. (If error appear) - El Forum - 11-07-2008

[eluser]Ignacio[/eluser]
Is not a bad idea, but I think that we should we sessions or something like that.