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

[eluser]Colin Williams[/eluser]
So, yeah, I was failing to pass $config from my constructor to CI_Upload's constructor...

I also updated it so the API remains the same, just call $this->upload->do_upload(), not ->do_multi_upload()

Code:
<?php

class MY_Upload extends CI_Upload {
  
   function MY_Upload($props = array())
   {
      parent::CI_Upload($props);
   }
  
   function do_upload($field = 'userfile')
   {
      $success = FALSE;
      if (isset($_FILES[$field]) and 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 it's magic on our pseudo file field
            $success = parent::do_upload($pseudo_field_name);
         }
      }
      else
      // Works just like do_upload since it's not an array of files
      {
         $success = parent::do_upload($field);
      }
      return $success;
   }
  
}

A few caveats that should be addressed: The $this->upload->data() method will only return data of the last file to be uploaded, not all files uploaded. If one of the fields errors, there is no way to know which individual one was erroneous. Not impossible problems to solve, but the code above has yet to address either of them. I'll work on rounding it out if I get some time, or need to do it myself.

For now, it might be good to just have unique names for x number of upload fields, then process each one individually.

Here's my controller:

Code:
&lt;?php

class Upload extends Controller {
  
   function Upload()
   {
      parent::Controller();
   }
  
   function index()
   {
      $data = array(
         'error' => array(),
         'upload_data' => array(),
      );
      
      $this->load->helper('form');
      
      $config['upload_path'] = 'uploads';
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '2048';
      $config['max_width'] = '1024';
      $config['max_height'] = '768';
      $this->load->library('upload', $config);
      
      if ( ! $this->upload->do_upload())
      {
         $data['error'] = array('error' => $this->upload->display_errors());
      }  
      else
      {
         $data['upload_data'] = $this->upload->data();
      }
      
      $this->load->view('sandbox/upload', $data);
      
   }
  
}

And my view:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
   &lt;title&gt;Multi Upload Test&lt;/title&gt;
   &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;/head&gt;
&lt;body&gt;
  
   &lt;? foreach ($error as $msg) : ?&gt;
   <div style="color: red; display: block; margin: 12px 0">&lt;?= $msg ?&gt;</div>
   &lt;? endforeach; ?&gt;
  
   &lt;? if (count($upload_data)) : ?&gt;
      <ul>
      &lt;?php foreach($upload_data as $item => $value):?&gt;
         <li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
      &lt;?php endforeach; ?&gt;
      </ul>
   &lt;? endif; ?&gt;
  
   <h1>Single File Upload</h1>
  
   &lt;?= form_open_multipart('sandbox/upload') ?&gt;
      <div>&lt;?= form_upload('userfile') ?&gt;</div>
      <div><button type="submit">Upload!</button></div>
   &lt;/form&gt;
  
   <h1>Multiple File Upload</h1>
  
   &lt;?= form_open_multipart('sandbox/upload') ?&gt;
      <div>&lt;?= form_upload('userfile[]') ?&gt;</div>
      <div>&lt;?= form_upload('userfile[]') ?&gt;</div>
      <div>&lt;?= form_upload('userfile[]') ?&gt;</div>
      <div><button type="submit">Upload!</button></div>
   &lt;/form&gt;
  
&lt;/body&gt;
&lt;/html&gt;


Messages In This Thread
Array to string conversion - by El Forum - 07-14-2008, 10:10 AM
Array to string conversion - by El Forum - 07-14-2008, 10:44 AM
Array to string conversion - by El Forum - 07-14-2008, 10:46 AM
Array to string conversion - by El Forum - 07-14-2008, 11:27 AM
Array to string conversion - by El Forum - 07-14-2008, 11:29 AM
Array to string conversion - by El Forum - 07-14-2008, 12:04 PM
Array to string conversion - by El Forum - 07-14-2008, 03:52 PM
Array to string conversion - by El Forum - 07-14-2008, 09:48 PM
Array to string conversion - by El Forum - 07-14-2008, 10:49 PM
Array to string conversion - by El Forum - 07-14-2008, 11:15 PM
Array to string conversion - by El Forum - 07-15-2008, 01:36 AM
Array to string conversion - by El Forum - 07-15-2008, 02:51 AM
Array to string conversion - by El Forum - 07-15-2008, 03:24 AM
Array to string conversion - by El Forum - 07-15-2008, 03:38 AM
Array to string conversion - by El Forum - 07-15-2008, 05:37 AM
Array to string conversion - by El Forum - 07-17-2008, 11:55 AM
Array to string conversion - by El Forum - 07-17-2008, 12:57 PM
Array to string conversion - by El Forum - 07-17-2008, 01:04 PM
Array to string conversion - by El Forum - 07-17-2008, 02:49 PM
Array to string conversion - by El Forum - 07-18-2008, 05:32 AM
Array to string conversion - by El Forum - 07-18-2008, 06:42 AM
Array to string conversion - by El Forum - 07-18-2008, 07:22 AM
Array to string conversion - by El Forum - 08-02-2008, 01:42 PM
Array to string conversion - by El Forum - 09-28-2008, 04:12 AM
Array to string conversion - by El Forum - 02-07-2009, 04:25 PM
Array to string conversion - by El Forum - 04-14-2010, 03:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB