Jump to content

very strange foreach problem...


richrock

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.