richrock Posted February 16, 2009 Share Posted February 16, 2009 I've made my function to upload images, and needed it for new items and editing current items. Now the only thing that is required is two id numbers, for defining the item and it's section. Not a problem, the same variables are passed through POST. So I have two functions - one is addNewItem() and the other is editCurrItem()... It doesn't matter that there are existing images, so I thought I could repeat the process in each form. However, add a new item allows upload of images, yet editing the item doesn't. Here is the image upload code that's causing the problem: foreach($_FILES["pictures"]["tmp_name"] as $key=>$error) { if ($error == UPLOAD_ERR_OK) { //Process image goes here - this bit works. } } Adding new images is fine, yet in editing, I get this : Warning: Invalid argument supplied for foreach() in /var/www/Admin_System/cons_functions.php on line 100 and my POST: [pictures] => Array ( [0] => 3123-2b.jpg [1] => 3123-2f.jpg [2] => 3123-2ffb.jpg ) I really don't understand why one set works, yet the other doesn't... The form elements are the same, the upload/resize process is exactly the same... So why am I getting a foreach error on edit rather than add new? TIA - This is really starting to annoy me... Link to comment https://forums.phpfreaks.com/topic/145465-very-strange-foreach-problem/ Share on other sites More sharing options...
genericnumber1 Posted February 16, 2009 Share Posted February 16, 2009 I think I've addressed this before, maybe it was with a different poster.. $_FILES["pictures"]["tmp_name"] is a string, not an array.. $_FILES['pictures'] is an array that just happens to have 'tmp_name' as an index for one of its elements... <?php foreach($_FILES["pictures"] as $value) { echo $value['tmp_name']; } or for that array dump you posted, you don't need to do $value['tmp_name'], just $value Link to comment https://forums.phpfreaks.com/topic/145465-very-strange-foreach-problem/#findComment-763711 Share on other sites More sharing options...
angelcool Posted February 16, 2009 Share Posted February 16, 2009 foreach($_FILES["pictures"]["tmp_name"] as $key=>$error) The foreach statement expects an array, try $_FILES['pictures']['name']. http://us.php.net/manual/en/features.file-upload.multiple.php Link to comment https://forums.phpfreaks.com/topic/145465-very-strange-foreach-problem/#findComment-763715 Share on other sites More sharing options...
richrock Posted February 16, 2009 Author Share Posted February 16, 2009 Thanks guys - managed to sort it - works for both now. Just one more thing, how could I tag each image name in the array with a unique suffix? For example, if I uploaded 1 & 2 out of the 3 images, I would have image_1.jpg, image_2.jpg... Or if I just uploaded the third image on it's own it would be image_3.jpg... Or even if I uploaded 1& 3, but left 2 : image_1.jpg, image_3.jpg... Especially as this is a feature because I'm displaying images of specific views, and they are uploaded according to the view? I've managed to prefix them with the thumb_ or main_ to show thumbnail or main image... Any ideas appreciated. Rich Link to comment https://forums.phpfreaks.com/topic/145465-very-strange-foreach-problem/#findComment-763738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.