Welcome Guest, Not a member yet? Register   Sign In
Multiple file upload problem
#1

[eluser]dallen33[/eluser]
I found this handy script on StackExchange that allows for multiple uploads. Except it doesn't work, for me, at least. In the HTML, I have a simple upload form that you can add more upload forms to. So you hit "Add upload" and another input is built. Simple stuff.

Here are the problems:
1. If I choose 1 file to upload, I get the following errors: Message: is_uploaded_file() expects parameter 1 to be string, array given and Message: Undefined offset: 1 repeated 5 times (in reference to the code below), and then shows Message: is_uploaded_file() expects parameter 1 to be string, array given again.
2. If I choose 2 files, I get Message: is_uploaded_file() expects parameter 1 to be string, array given twice.

The error array is always returned as You did not select a file to upload..

What am I doing wrong? Any ideas?

Here's my controller:
Code:
$dir = random_string('alnum', 10);
mkdir('/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir);

$this->load->library('upload');
$error = 0;

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];
    
    $config['upload_path']                         = '/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir;
    $config['allowed_types']                    = 'jpg|jpeg|gif|png';
    
    $this->upload->initialize($config);

    if ($this->upload->do_upload('userfile')):
        $error += 0;
    else:
        $error += 1;
    endif;
endfor;

if ($error > 0):
    $error = array('error' => $this->upload->display_errors());
    print_r($error);
else:
    $data = array('upload_data' => $this->upload->data());
    print_r($data);
endif;

And my &lt;input&gt;'s look like this:
Code:
&lt;input id="userfile1" name="userfile[]" type="file" /&gt;
#2

[eluser]pickupman[/eluser]
It looks like you are passing the filename array to the do_upload() method. You are setting them to userfile, but not referencing it that way.
Try changing to
Code:
$this->upload->do_upload('userfile')
#3

[eluser]dallen33[/eluser]
Thanks pickupman. I've revised my script because I noticed some errors above. Now, when I upload one or more files, I get my array returned, but it only uploads one file:

Code:
Array ( [upload_data] => Array ( [file_name] => bg.png [file_type] => image/png [file_path] => /Applications/MAMP/htdocs/extras/uploads/temp/Q61Q3s8sie/ [full_path] => /Applications/MAMP/htdocs/extras/uploads/temp/Q61Q3s8sie/bg.png [raw_name] => bg [orig_name] => bg.png [client_name] => bg.png [file_ext] => .png [file_size] => 85.72 [is_image] => 1 [image_width] => 300 [image_height] => 250 [image_type] => png [image_size_str] => width="300" height="250" ) )
#4

[eluser]d1a8lo24[/eluser]
Try the following

Code:
// Do your configuration
$config['upload_path']        = $this->portfolio_path.$row->profile_account.'/';
$config['allowed_types']    = 'jpg|jpeg|gif';
$config['encrypt_name']        = TRUE;

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

// This is my own way of doing it rename the keys and fields for the file names you use.
$pictures = array(
            'userfile_1' => $_FILES['userfile_1'],
            'userfile_2' => $_FILES['userfile_2'],
            'userfile_3' => $_FILES['userfile_3'],
            'userfile_4' => $_FILES['userfile_4'],
            'userfile_5' => $_FILES['userfile_5'],
            'userfile_6' => $_FILES['userfile_6'],
            'userfile_7' => $_FILES['userfile_7'],
            'userfile_8' => $_FILES['userfile_8']

            );
// You don't need to use these.
$i = 0;
$e = 0;

foreach($pictures as $field => $picture)
{
    // If a file was not submitted do not proccess
    if($picture['error'] == 0)
    {
        // If there is an error start image counter
        if( ! $this->upload->do_upload($field) )
        {
            $i++;    
        }
        
        // Use this to get the new image info if you need to insert it in a database
        $imageinfo = $this->upload->data();
                
    } else {
        $e++;    
    }
}
#5

[eluser]toopay[/eluser]
$imageinfo, should be an array, or it will only returned the last upload data
Code:
$imageinfo[] = $this->upload->data();
#6

[eluser]d1a8lo24[/eluser]
Yes it is an array and depending on what you are doing you can use either a $var or $var_array[].

In my code I use the $imageinfo, in the loop to be inserted in a database so for each loop and for each file there will be a database insert.

Remember this has to be done in the loop.

As for $var_array[] you wait until you finish the loop and then in order to do anything with the info you will have to loop again after the file upload loop and before returning anything or you may want to return that info.

I know everyone has their own way of coding, I have use my code in applications and as long as you know whats going on in the loop you shouldn't have a problem, you can use either my code or what toopay said just remember when to use it.

my code again with a database insert and you can do as many as you want do 3 table inserts to manage my photo app
Code:
// Do your configuration
$config['upload_path']        = $this->portfolio_path.$row->profile_account.'/';
$config['allowed_types']    = 'jpg|jpeg|gif';
$config['encrypt_name']        = TRUE;

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

// This is my own way of doing it rename the keys and fields for the file names you use.
$pictures = array(
            'userfile_1' => $_FILES['userfile_1'],
            'userfile_2' => $_FILES['userfile_2'],
            'userfile_3' => $_FILES['userfile_3'],
            'userfile_4' => $_FILES['userfile_4'],
            'userfile_5' => $_FILES['userfile_5'],
            'userfile_6' => $_FILES['userfile_6'],
            'userfile_7' => $_FILES['userfile_7'],
            'userfile_8' => $_FILES['userfile_8']

            );
// You don't need to use these.
$i = 0;
$e = 0;

foreach($pictures as $field => $picture)
{
    // If a file was not submitted do not proccess
    if($picture['error'] == 0)
    {
        // If there is an error start image counter
        if( ! $this->upload->do_upload($field) )
        {
            $i++;    
        }
        
        // Use this to get the new image info if you need to insert it in a database
        $imageinfo = $this->upload->data();
        
        // Database insert
        $data = array(
        'image_name'        => $imageinfo['file_name'],
        'image_created_on'    => now(),
        'image_updated_on'    => now()
        );
                    
    $this->db
         ->insert('your_image_table', $data);
    // If you insert more info another table you can use this to reference the last insert
        // id and the after you will do another database insert.            
    $image_id = $this->db->insert_id();
                
    } else {
        $e++;    
    }
}

Anyway make sure how arrays and loops work otherwise you will not like the results.




Theme © iAndrew 2016 - Forum software by © MyBB