<?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?
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);
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++;
}
}
?>