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

[eluser]Mitja B.[/eluser]
hmm i try like this

VIEW:
Code:
<?= form_open_multipart('Upload/picupload');?>
<table class="arrangementPluspayment">
    <tr><td>Fotka: &lt;input type="file" name="userfile[]" /&gt;&lt;/tr>
    <tr><td>Fotka: &lt;input type="file" name="userfile[]" /&gt;&lt;/tr>
</table>
<tr>
    <td>&lt;input type="submit" value="Shrani" /&gt;&lt;/td>
</tr>
&lt;?= form_close();?&gt;

CONTROLLER:

Code:
function picupload()
{
//Load Model
$this->load->model('Process_image');

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '2048'; //2 meg
        
$this->load->library('upload', $config);

if (!$this->upload->do_multi_upload())
{
    print $this->upload->display_errors();
}    
else
{
    print 'Thank You, Files Upladed!';
}
}

but picture is not uploaded and i get this error The upload path does not appear to be valid. Th uploads dir is valid and exist, i do not know why i get this error.
#12

[eluser]Colin Williams[/eluser]
Perhaps $config['upload_path'] = './uploads/'; is wrong?

Try it without the multiple files and the original do_upload() method. If it works then, it's definitely something wrong with my extension.
#13

[eluser]Colin Williams[/eluser]
Ahah! Looks like my constructor is bad. New code in just a bit...
#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:
&lt;?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;
#15

[eluser]Mitja[/eluser]
cool, thx for all your help
#16

[eluser]CARP[/eluser]
Hi Colin
I've tried your library, and it is working great!

The only problem I've found so far is

I have a little backend for adding products. Each product can have from 1 to 5 images. 1 is mandatory for each product being added, but I want to make the rest upload fields optional.

The library keeps showing the "You did not select a file to upload." error message for each field I didn't select image for, although the uploads are working fine for the fields where I selected an image

All the fields are named "usefile[]", and I've made the first 1 field mandatory (through wforms JS validation script)

Do you know how could I tweak your library so only the first upload field is mandatory and so it works w/o showing errors, no matter how many images user uploads?

Thanks for the good job,
#17

[eluser]Mitja B.[/eluser]
CARP, you can use [] for name. If i use array i get error of array to string problem. I try Colin Williams last code but the same.
#18

[eluser]CARP[/eluser]
Juve fan
I've used this for naming all the upload fields... like the example
userfile[]

Maybe I didn't understand your reply, or don't know what you're refering to
#19

[eluser]Mitja B.[/eluser]
i mean that i want to use model below that i get random uniqe number for image. I call it
but only third image is processed with this library not all pictures. If i use only two pictures then no picture is processed. Is it possible to use it with your code?

Code:
if ( ! $this->upload->do_upload())
      {
         $data['error'] = array('error' => $this->upload->display_errors());
      }  
      else
      {
         $this->Process_image->process_pic();
         $data['upload_data'] = $this->upload->data();
      }



MODEL:

Code:
class Process_image extends Model {
    
    function Process_image()
    {
        parent::Model();
        
        $this->load->library('image_lib');
        //Generate random Activation code
        
        function generate_code($length = 10){
    
                if ($length <= 0)
                {
                    return false;
                }
            
                $code = "";
                $chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
                srand((double)microtime() * 1000000);
                for ($i = 0; $i < $length; $i++)
                {
                    $code = $code . substr($chars, rand() % strlen($chars), 1);
                }
                return $code;
            
                }

    }

function process_pic()
    {  
        //Connect to database
        $this->load->database();
        
        //Get File Data Info
        $uploads = array($this->upload->data());
        
        $this->load->library('image_lib');

        //Move Files To User Folder
        foreach($uploads as $key[] => $value)
        {
            
                        //Gen Random code for new file name
            $randomcode = generate_code(12);
            
            $newimagename = $randomcode.$value['file_ext'];
            
            //Creat Thumbnail
            $config['image_library'] = 'GD2';
            $config['source_image'] = $value['full_path'];
            $config['create_thumb'] = TRUE;
            $config['thumb_marker'] = '_tn';
            $config['master_dim'] = 'width';
            $config['quality'] = 75;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 175;
            $config['height'] = 175;
            $config['new_image'] = '/pictures/'.$newimagename;

            //$this->image_lib->clear();
            $this->image_lib->initialize($config);
            //$this->load->library('image_lib', $config);
            $this->image_lib->resize();
            
            //Move Uploaded Files with NEW Random name
            rename($value['full_path'],'/pictures/'.$newimagename);
            
            //Make Some Variables for Database
            $imagename = $newimagename;
            $thumbnail = $randomcode.'_tn'.$value['file_ext'];
            $filesize = $value['file_size'];
            $width = $value['image_width'];
            $height = $value['image_height'];
            $timestamp = time();
            
            //Add Pic Info To Database
            $this->db->set('imagename', $imagename);
            $this->db->set('thumbnail', $thumbnail);
            $this->db->set('filesize', $filesize);
            $this->db->set('width', $width);
            $this->db->set('height', $height);
            $this->db->set('timestamp', $timestamp);
            
            //Insert Info Into Database
            $this->db->insert('pictures');

        }
        
        
        
    }
#20

[eluser]Mitja[/eluser]
you want to get random number instead of real number and thumbnail of it. Will try if works for me.




Theme © iAndrew 2016 - Forum software by © MyBB