2
<?php
ini_set('error_reporting', E_ALL); // error reporting

// save uploaded files
$i = 1;
while (list ($item, $value) = each ($_FILES)){
    if(substr($item, 0, 11) == 'imageloader'){
        $fileName = $i.'.'.substr($item, 11, 3); // make file name
        move_uploaded_file($value['tmp_name'], $fileName); // save file
        $i++;
    }
}
?>

I need to use foreach. I have tried but it just doesn't work. Can anyone help me change the EACH to a FOREACH loop?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Kim Anov
  • 53
  • 1
  • 6

2 Answers2

4

Change while (list ($item, $value) = each ($_FILES)){

to

foreach( $_FILES as $key => $file ) { }

the syntax above is really old and shouldn't be used.

Moreover you don't get extension properly in order to get extension use

$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
Robert
  • 19,800
  • 5
  • 55
  • 85
0

these are foreach syntaxes:

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

in your case it would be:

<?php
ini_set('error_reporting', E_ALL); // error reporting

// save uploaded files
$i = 1;
foreach ($_FILES as $item => $value)){
    if(substr($item, 0, 11) == 'imageloader'){
        $fileName = $i.'.'.substr($item, 11, 3); // make file name
        move_uploaded_file($value['tmp_name'], $fileName); // save file
        $i++;
    }
}
?>
mehrdadep
  • 972
  • 1
  • 15
  • 37