How do I access the array when I am outside of the foreach block?

I am trying to process and display two images. The processing and the moving of the two images to a folder works. Now I am trying to display both images in the same web page that processed them. I just need their file names and directories.

I think I have narrowed down the problem so I have removed a lot of code to make it simpler. It looks to me that the array in $ndfilenm disappears as it leaves the first foreach block.

How do I resolve this; Is there a way to access that array later in the second foreach block to display the image? Thanks.

<?php
foreach ($_FILES['image']['name'] as $number => $file) {

// A bunch of php here.

$ndfilenm = UPLOAD_DIR . $filenm;

var_dump($ndfilenm); // dumps out two image directory and names, "great".
}
var_dump($ndfilenm); // dumps out only the last image directory and name, "not both".
?>

Some html here.

<?php
// Warning: Invalid argument supplied for foreach()
if (isset($ndfilenm)) {
  foreach ($ndfilenm as $item) {
    echo "<img src= '$item' />";
	}
  }
?>

Kalon, I thank you for your help.

That suggestion

$ndfilenm[] = UPLOAD_DIR . $filenm;

seems to be working at the moment.

It did not work the first time because I put brackets next to all $ndfilenm variables. I guess brackets are only used when putting info into a variable as apposed to accessing info from.

What do you mean by:

…the array in $ndfilenm…

because in your code

 
$ndfilenm = UPLOAD_DIR . $filenm;

$ndfilenm is not an array because an array needs a key (either indexed or associative)

Should

$ndfilenm = UPLOAD_DIR . $filenm;

be

$ndfilenm[] = UPLOAD_DIR . $filenm;

to append a new element to the array?

Maybe this isn’t clear because you haven’t posted all your code.

Here is some more code.


$udfilenm = $_FILES['image']['tmp_name'][$number];

if ($typeok) {
		move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
		$ndfilenm = UPLOAD_DIR . $filenm;
	}

you didn’t answer my first question.

Sorry. We must have been posting at the same time I did not see all your message.


$udfilenm = $_FILES['image']['tmp_name'][$number];

if ($typeok) {
        move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
        $ndfilenm = UPLOAD_DIR . $filenm;
    } 

I am moving the two image files (an array in $udfilenm) to a folder and changing the two tmp names to permanent names with their directory addresses appended to each. Then I am saving this array in $ndfilenm. (nd) stands for new directory.

I tried this:

$ndfilenm[] = UPLOAD_DIR . $filenm;

but I got some errors. $ndfilenm goes through an image reduction process next and then that’s the end of the first foreach block.