Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Multiple File Upload not working

I am trying upload multiple file in a folder but it's giving the error "You did not select a file to upload."
A PHP Error was encountered

Severity: Warning

Message: is_uploaded_file() expects parameter 1 to be string, array given

Filename: libraries/Upload.php

Line Number: 412

Backtrace:

File: C:\wamp\www\mshaadi\application\controllers\Email.php Line: 55 Function: do_upload

File: C:\wamp\www\mshaadi\index.php Line: 293 Function: require_once

Controller

$conf['upload_path'] = './images';
    $conf['allowed_types'] = 'doc|docx|pdf|jpg|gif|jpeg|png';
    $conf['max_size'] = '9999000';
    $conf['max_width'] = '1024';
    $conf['max_height'] = '768';
    $conf['overwrite'] = TRUE;
    $this->load->library('upload');
    foreach ($_FILES as $fieldname => $fileObject){
            $this->upload->initialize($conf);
        if (!empty($fileObject['name'])){
            if (!$this->upload->do_upload($fieldname)){
                $error = $this->upload->display_errors();
                print_r($error);
            }else{
                 print_r("done");
            }
        }else {
            print_r("no");
        }
    }

view

<div class="form-group col-md-12">
        <label for="Attach"><strong>Add Attachment</strong><br></label>
        <input type="file" class="btn btn-default btn-file" name="atta[]" id="Attach" multiple="multiple"> 
</div>
like image 600
Sachin Marwha Avatar asked Nov 18 '15 16:11

Sachin Marwha


People also ask

What is $_ files in PHP?

PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.


3 Answers

Try like this,

function upload_files()
{       
    $config = array();
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

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

    $files = $_FILES;
    for($i=0; $i< count($_FILES['userfile']['name']); $i++)
    {           
        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

        $this->upload->initialize($config);
        $this->upload->do_upload();
    }
}
like image 63
Niranjan N Raju Avatar answered Oct 20 '22 19:10

Niranjan N Raju


this is working

 function do_upload()
    {       
        $this->load->library('upload');

        $files = $_FILES;
        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i<$cpt; $i++)
        {           
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload();
        }
    }

    private function set_upload_options()
    {   
        //upload an image options
        $config = array();
        $config['upload_path'] = './Images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = '0';
        $config['overwrite']     = FALSE;

        return $config;
    }
like image 30
Sachin Marwha Avatar answered Oct 20 '22 19:10

Sachin Marwha


I'm going to add my answer as a fuller explanation to @SachinMarwa's own answer. The code I am submitting is not different code, but rather adds some lines and specifics not mentioned in his answer.

Even though it looks like his answer is technically correct, and perhaps the answers above his are also correct in their own way, they didn't work for me. I had to study this problem to find out what is really going on, and I learned enough about the process to understand how to write my own solution.

First of all, refer to Nana Partykar's comment, "In your controller, i'm not able to see any is_uploaded_file() function ?" That comment tells us that people are misunderstanding the two files which have similar names, but are different. I know, because for a while I thought they must be referring to the same file, the controller file (named "Uploader.php"). I can see almost all of these questions refer to the same "How to upload multiple files using Ajax" tutorial somewhere out there, my own version, included. The code we are all using is exactly the same.

But, the controller file is "Uploader.php". Where you see $this->upload->do_upload() or $this->upload->do_upload('userfile') or even $this->upload->do_upload('files'), this is referring to a system/library module file named "Upload.php". Notice that before you call the do_upload() function, you have to invoke this line: $this->load->library('upload', $config);

Sachin Marwha gave us a for loop that loops through the $_FILES['userfile'] array. Let's say you upload three pictures. Each $_FILES['userfile'] element is itself made up of 5 "properties": name, type, tmp_name, error, size. You can see these $_FILE properties on PHP.

You only want to pass one file at a time to do_upload(). You don't want to pass all three (or even 20) files at a time to do_upload. That means you have to break down the $_FILES['userfile'] array into individual files before you call do_upload(). To do this, I make a $_FILES['f'] element of the $_FILES array. I figured this out by setting breakpoints in the system/library/Upload.php file, in the do_upload($file = 'userfile') function, to see where I was getting the infamous “did not select a file to upload” that everybody (including myself) keeps complaining about. That function, you will find, uses the original $_FILES array your form sends to your controller. But it really only uses the name of the input type=file from your form. If you don't tell it the name of the form input, it will default to $_FILES['userfile']. As it turns out, that was my biggest problem, because if I used the name of my input field, well, that field passes an array or a collection of files, not just a single file. So I had to make a special $_FILES['f] element, and ONLY pass $_FILES['f'].

Here's the way I am doing it, and believe me, I tried all of the versions on this page and others, not just one StackOverflow, but other tutorials, as well:

    $cpt = count($_FILES['userfile']['name']);
    for($i=0; $i < $cpt; $i++)
    {
        unset($config);
        $config = array();
        $config['upload_path']   = $path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = FALSE;
        $config['file_name'] = $_FILES['userfile']['name'][$i];

        // Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop.
        $_FILES['f']['name'] =  $_FILES['userfile']['name'][$i];
        $_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
        $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
        $_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
        $_FILES['f']['size'] = $_FILES['userfile']['size'][$i];

        $this->load->library('upload', $config);
        $this->upload->initialize($config);            
        if (! $this->upload->do_upload('f'))
        {
            $data['errors'] = $this->upload->display_errors();
        }
        else
        {
            $data['errors'] = "SUCCESS";
        }

        unset($config);
        $config = array();
        $config['image_library']    = 'gd2';
        $config['source_image']     = $path . $_FILES['userfile']['name'][$i];
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']   = TRUE;
        $config['thumb_marker']     = '.thumb';
        $config['width']            = 100;
        $config['height']           = 100;

        $this->load->library('image_lib', $config);
        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $types = array('.jpg');
    }        

Where it unsets the $config array within the for i loop, and then remakes the $config array, that's the part that makes the thumbnail images for each picture file.

The Complete Controller Upload Function:

    public function upload_asset_photo()
    {
        $data = array();
        $dateArray = explode("/",$this->input->post('date'));
        $date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day
        $cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys.
        $padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003"
        $path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path
        if (!is_dir($path)) {
            mkdir($path,0755,true); //makes the ile path, if it doesn't exist
        }

        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i < $cpt; $i++)
        {
            unset($config);
            $config = array();
            $config['upload_path']   = $path;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = FALSE;
            $config['file_name'] = $_FILES['userfile']['name'][$i];

            $_FILES['f']['name'] =  $_FILES['userfile']['name'][$i];
            $_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
            $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
            $_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
            $_FILES['f']['size'] = $_FILES['userfile']['size'][$i];

            $this->load->library('upload', $config);
            $this->upload->initialize($config);            
            if (! $this->upload->do_upload('f'))
            {
                $data['errors'] = $this->upload->display_errors();
            }
            else
            {
                $data['errors'] = "SUCCESS";
            }

            unset($config);
            $config = array();
            $config['image_library']    = 'gd2';
            $config['source_image']     = $path . $_FILES['userfile']['name'][$i];
            $config['create_thumb']     = TRUE;
            $config['maintain_ratio']   = TRUE;
            $config['thumb_marker']     = '.thumb';
            $config['width']            = 100;
            $config['height']           = 100;

            $this->load->library('image_lib', $config);
            $this->image_lib->clear();
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $types = array('.jpg');
        }        

        header('Content-Type: application/json');
        echo json_encode($data);
    }
like image 2
TARKUS Avatar answered Oct 20 '22 17:10

TARKUS