How to extract strings from array and store in variables

I have an array $filenm that can hold one two or three image names. I am trying to extract those image names, put them into individual variables like $image01, $image02, $image03 and then insert them into a database.

How do I go about accomplishing this? I tried extract(), foreach(), and list() in an attempt to verify the image names in the array.

extract() echoed Warning: extract() : First argument should be an array
echoed $0 $1 $2

foreach() echoed Warning: Invalid argument supplied for foreach()

list() echoed the first three letters of the first image name.

Thanks for advice.

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

   $filenm = str_replace(' ', '_', $file);

   extract($filenm);
   echo "$0, $1, $2";

   foreach ($filenm as $no => $image)
   while (list($no, $image) = each($filenm))
   echo "$no: $image";

   list($a,$b,$c) = $filenm;
   echo "$a $b $c";

}

Thanks for your help. I think I got this part to work.


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

   $filenm[] = str_replace(' ', '_', $file['name']);

} 

Thanks for your response.

I thought

foreach ($_FILES['image']['name'] as $number => $file) 

put an array of image names into $file.

Plus I thought I needed

$query = “INSERT INTO pics values (‘$image01’,‘$image02’,‘$image03’)”;

It’s not clear to me what you’re trying to do.

If $filenm is an array with the filenames, you don’t need to extract the filenames to variables, you can just use foreach() to insert them in the DB one by one:

foreach($filenm as $filename) {
//SQL query, like INSERT INTO images_table (filename) VALUES ('$filename')
//don't forget to escape the values
}

If you have $_FILES, it’s slightly different:

foreach($_FILES as $file) {
$filename = $file['name'];
//Similar SQL query
}

But in your code, $filenm isn’t an array, because since $file is a string, str_replace() also returns a string.

To make $file an array, should I do this?


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

   $filenm = str_replace(' ', '_', $file['name']);

}

Its late so this is probably wrong:


$iX=0;
$aFN = array();
foreach ($_FILES['image'] as $key => $value) {
  $aFN[$iX++] = $value['name'];
}
list($a,$b,$c) = $aFN;

I think the _FILES array is 2d: http://www.php.net/manual/en/features.file-upload.post-method.php