• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI 1.7 multi file upload. Possible?

#1
I've been trying to do a multi file upload with codeigniter 1.7. I'm having issues to say the least!
I know its an old version but thats what ive got to work with.
Can I loop the $_FILES array and upload each one? It doesnt seem to work.
Heres what ive got?
Code:
       $dir_name = $this->input->post('dir');

   $file_path = "./system/application/views/courses/course/";

   $full_file_path = $file_path . $dir_name;


   if (!file_exists($full_file_path)) {
       mkdir($full_file_path, 0777, true);
   }

   $config['upload_path'] = $full_file_path;
   $config['allowed_types'] = 'php|js|html';

   $this->load->library('upload');

   foreach ($_FILES as $files => $fileObject)  //
   {
       if (!empty($fileObject['name']))
       {
           $this->upload->initialize($config);
           if (!$this->upload->do_upload($files))
           {
               $errors = $this->upload->display_errors();
           }
           else
           {
               echo "It worked";
           }
       }
   }
Is this posssible?
This code runs but doesnt upload the files?!
Reply

#2
I was about to have a play with this (I have not done it yet) - might be helpful.

http://blueimp.github.io/jQuery-File-Upload/

Personally, I hate file uploading as it is so open to abuse or misuse.

Best wishes,

Paul.
Reply

#3
I just wrapped up writing this section in my book, actually. I don't have a 1.7 codebase lying around, but I don't think it changed that much.

The issue is that CI's Upload library doesn't support multiple files with the same name. Additionally, the $_FILES array likely isn't structured like you expect it to be. See the official PHP docs for an example. The simplest solution that I could come up with was to modify the $_FILES array directly to have a unique file name and data in a way that the Upload library can deal with it. The following function will do that for you:

Code:
public function restructureFilesArray($files)
    {
        $new_files = array();

        foreach ($files as $name => $file)
        {
            $file_count = count($file['name']);
            $file_keys = array_keys($file);

            $temp = [];

            for ($i=0; $i < $file_count; $i++)
            {
                foreach ($file_keys as $key)
                {
                    $temp[$key] = $file[$key][$i];
                }

                $new_files[$name.$i] = $temp;
            }
        }

        return $new_files;
    }

You can then use it in your controller something like:

Code:
public function index()
    {
        if (! empty($_FILES))
        {
            $config['upload_path']          = APPPATH.'cache';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

            $this->load->library('upload', $config);

            $_FILES = $this->restructureFilesArray($_FILES);

            foreach ($_FILES as $file => $arr)
            {
                if ( ! $this->upload->do_upload($file))
                {
                    echo "Unable to upload {$file}.<br/>";
                }

                echo "{$file} uploaded.<br/>";
            }
        }

        $this->load->view('uploads');
    }
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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