Array to string conversion - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Array to string conversion (/showthread.php?tid=9952) |
Array to string conversion - El Forum - 07-14-2008 [eluser]Mitja[/eluser] Code: foreach($_FILES as $key => $value) if ( !$this->upload->do_upload($key)) show me an error of Quote:A PHP Error was encountered $key has pluspayment_title_photo value View is: <tr><td>Fotka: <input type="file" name="pluspayment_title_photo[]" /></tr> what i am doing wrong? Array to string conversion - El Forum - 07-14-2008 [eluser]nirbhab[/eluser] dude as far as my understanding is concerned $key is an Array(). Code: if( ! empty($key['name'])) 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/'; Load upload library once with its configurations. Array to string conversion - El Forum - 07-14-2008 [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. Array to string conversion - El Forum - 07-14-2008 [eluser]Mitja[/eluser] now if i make Code: if(!empty($_FILES[$key])) i still get the same Message: Array to string conversion Array to string conversion - El Forum - 07-14-2008 [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. Array to string conversion - El Forum - 07-14-2008 [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 { 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. Array to string conversion - El Forum - 07-14-2008 [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. Array to string conversion - El Forum - 07-14-2008 [eluser]Colin Williams[/eluser] Make sure my code goes in application/libraries/MY_Upload.php and leave system/libraries/Upload.php alone Array to string conversion - El Forum - 07-14-2008 [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? Array to string conversion - El Forum - 07-14-2008 [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! |