I just can't seem to figure out arrays

I am uploading two images. They pass through this script and get moved to a new folder. I am trying to grab the names from these two images and insert them into a data base. As is I get this error.

Warning: Invalid argument supplied for foreach() in /test03.php on line 32

Is there no array in $filenm? Or is there something wrong with my foreach statement? Thanks.

define('UPLOAD_DIR', 'upload_test/');

$today = @date('Y-m-d-g-i');

if( isset($_POST['submitted']) ){
	
	foreach ($_FILES['image']['name'] as $number => $file) {
	
	$filenm = str_replace(' ', '_', $file);
	
	$udfilenm = $_FILES['image']['tmp_name'][$number];
	
	move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
	
    }
	
	foreach($filenm as $filename) {
		$query = "INSERT INTO adpics VALUES ('','$filename','','$today')";
		if( !$db->query( $query ) ){
		$errString .= "<p>error with database.</p>";
		}
	} 	
}

$filenm isn’t an array, which is way foreach() throws an error. You could create an array out of it, but in this case you could also move the body of the 2nd foreach into the body of the 1st foreach, change variables where needed, and that should work as well :slight_smile:

You mean the one where I told the OP to remove the after $filenm? :wink:

This seems to be the same problem as in another thread.

Let Google be you friend :google:

Here is a very good tutorial on what php arrays are and how to use them.

print_r() – learn it, love it and embrace it.

I wouldn’t change $filenm to an array if I were you, because you’re using it later in the move_uploaded_file function and that makes it harder.

Instead, I’d do it as follows:


define('UPLOAD_DIR', 'upload_test/');
$today = date('Y-m-d-g-i');

if (isset($_POST['submitted'])) {
	
	$filenames=array();
	foreach ($_FILES['image']['name'] as $number => $file) {
		$filenm = str_replace(' ', '_', $file);
		$udfilenm = $_FILES['image']['tmp_name'][$number];
		move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
		$filenames[]=$filnm;
	}

	foreach($filenames as $filename) {
		$query = "INSERT INTO adpics VALUES ('','$filename','','$today')";
		if( !$db->query( $query ) ){
			$errString .= "<p>error with database.</p>";
		}
	}
	
}

For completeness sake, this is what I proposed you do instead:


define('UPLOAD_DIR', 'upload_test/');
$today = date('Y-m-d-g-i');

if (isset($_POST['submitted'])) {
	
	foreach ($_FILES['image']['name'] as $number => $file) {
		$filenm = str_replace(' ', '_', $file);
		$udfilenm = $_FILES['image']['tmp_name'][$number];
		move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
		$query = "INSERT INTO adpics VALUES ('','$filenm','','$today')";
		if( !$db->query( $query ) ){
			$errString .= "<p>error with database.</p>";
		}
	}
	
}

:slight_smile:

PS. When you’re done with this, you should read about SQL Injection (google it)

I don’t know.

The OP’s code looked similar to that in a thread I also replied to.

It might not even had been on these forums :smiley:

Thanks ScallioXTX.

I tried your first example and it worked perfectly.

I did more var_dumps($filenm); after move_uploaded_file and get this: string(0) “”.

I think I figured out why. I have three image slots but have only been using two for testing. So the third image name is not there ( string(0) “”). When I test with all three images being uploaded, var_dumps out only the third image.

It is kind of strange to me that $filenm seems to loose the array when passing through move_uploaded_file and then magically stores an array in $filenames=$filnm;. Now $filenames has the array.

It is a little confusing. My PHP books don’t always help with these issues.

Thanks for your help. I still have a lot more to integrate into this script. I think my better understanding will help me.

Thanks Kalon and oddz. I will investigate your suggestions.

Thanks for your response.

I just experimented with placing var_dump($filenm); at different locations and discovered that the array in $filenm empties or disappears after passing


move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);

Can you tell me why $filenm is not an array and show me how to create an array out of $filenm just so I can learn. Then maybe I try your second solution. Thanks for your help.