Welcome Guest, Not a member yet? Register   Sign In
Array to string conversion
#1

[eluser]Mitja[/eluser]
Code:
foreach($_FILES as $key => $value)
{
    if( ! empty($key['name']))
    {
    $this->upload->initialize($config);
    $data['title'] = 'Files Upload!';
    if ( !$this->upload->do_upload($key))
        {

if ( !$this->upload->do_upload($key)) show me an error of

Quote:A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: libraries/Upload.php

Line Number: 152

$key has pluspayment_title_photo value

View is:

<tr><td>Fotka: &lt;input type="file" name="pluspayment_title_photo[]" /&gt;&lt;/tr>

what i am doing wrong?
#2

[eluser]nirbhab[/eluser]
dude as far as my understanding is concerned $key is an Array().
Code:
if( ! empty($key['name']))
check this piece of code.
This is Array() for sure.
Please check the CI User Guide, there is a function do_upload().
Call this function in for loop for multiple uploading.
Code:
$config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
Don't use this piece of code again and again, as this one is loading the upload library.
Load upload library once with its configurations.
#3

[eluser]Colin Williams[/eluser]
$key is a string, not an array. It is ALWAYS this way when you use the foreach construct in that way ($hash => value). The PHP error is very clear on that (it even points you to the exact line.. how handy).

You need to do $_FILES[$key] not $key['name']. $_FILES is the array you are looping through, afterall.
#4

[eluser]Mitja[/eluser]
now if i make

Code:
if(!empty($_FILES[$key]))

i still get the same Message: Array to string conversion
#5

[eluser]Colin Williams[/eluser]
Ah.. my bad. The Upload library is throwing the error, not your code. It expects the field to be a string, not an array, and for some reason, it doesn't check first. I would just name your upload field(s) something like upload_1, upload_2, etc, and then call do_upload in a loop for each one that has a non empty value.

I'm surprised the upload class doesn't accommodate arrays of files. It's not like it's an exotic process.
#6

[eluser]Colin Williams[/eluser]
This is completely untested but I think it will work. A good starting point nonetheless

Code:
class MY_Upload extends CI_Upload {
  
   function MY_Upload()
   {
      parent::CI_Upload();
   }

   function do_multi_upload($field = 'userfile')
   {
      $success = FALSE;
      if (is_array($_FILES[$field]['error']))
      {
         // Create a pseudo file field for each file in our array
         for ($i = 0; $i < count($_FILES[$field]['error']); $i++)
         {
            // Give it a name not likely to already exist!
            $pseudo_field_name = '_psuedo_'. $field .'_'. $i;
            // Mimick the file
            $_FILES[$pseudo_field_name] = array(
               'name' => $_FILES[$field]['name'][$i],
               'size' => $_FILES[$field]['size'][$i],
               'type' => $_FILES[$field]['type'][$i],
               'tmp_name' => $_FILES[$field]['tmp_name'][$i],
               'error' => $_FILES[$field]['error'][$i]
            );
            // Let do_upload work its magic on our pseudo file field
            if (!$this->do_upload($pseudo_field_name))
            {
               $success = FALSE;
               break;
            }
            else
            {
               $success = TRUE;
            }
         }
      }
      else
      // Works just like do_upload since it's not an array of files
      {
         $success = $this->do_upload($field);
      }
      return $success;
   }
}

What I'm doing here is making copies of files from the array of files, then passing these copies onto the existing do_upload() method to process them. It's better than overriding the beast that is do_upload(), and it's probably a bit future-proof/less-hackish to not override a core library method.
#7

[eluser]Mitja[/eluser]
Tzx you Colin Williams but i still have one problem

Fatal error: Class 'CI_Upload' not found. I add $this->load->library('upload'); but not working.
#8

[eluser]Colin Williams[/eluser]
Make sure my code goes in application/libraries/MY_Upload.php and leave system/libraries/Upload.php alone
#9

[eluser]Mitja B.[/eluser]
this is just what i am looking for. Colins how can i use this, i am n00b can you give me an example?
#10

[eluser]Colin Williams[/eluser]
IT IS NOT TESTED. But, follow the same guidelines for $this->upload->do_upload() as outlined in the User Guide, except, call $this->upload->do_multi_upload(). You can call it even if your field is NOT an array (name="userfile[]").

Please test this and let me know if it works!




Theme © iAndrew 2016 - Forum software by © MyBB