i like to upload some images to a directory with the help of arrays. therefore i have this code:
$allowedExtensions = array(\'jpg\', \'jpeg\', \'png\', \'bmp
Why are you accessing the $_FILES
superglobal array as a three dimensional array?
if you want the file name of the file uploaded from an <input type="file" name="image"/>
all you have to do is $name = $_FILES[ 'image' ][ 'name' ]
there is no need for the [ $i ]
at the end.
You need to loop over the entries in $_FILES like this:
foreach ( $_FILES as $inputName => $fileData )
{
// $inputName is be 'image` for the first input and so on,
// $fileData will be an array of the attributes [ 'name', 'tmp_name', ... ]
}
First of, I would name the uploaded fields seperatly. E.g. name the first field <input name="image_1" type="file" />
and the second <input name="image_2" type="file" />
. Then you can iterate over the $_FILES
array instead:
foreach($_FILES as $fileId => $file){
//make sure it's a file you want (optional)
if(!preg_match("/^image\_\d+$/",$fileId){
continue;
}
//the rest of your code from the for loop
}
Secondly, you need to make sure that your form's enctype is multipart/form-data
.
Does any of this help?
I'm not sure if this may solve it for you, but you can try to convert these into "normal" $_FILES
values.
$arr_files = @$_FILES['image'];
$_FILES = array();
foreach(array_keys($arr_files['name']) as $h)
$_FILES["image_{$h}"] = array( 'name' => $arr_files['name'][$h],
'type' => $arr_files['type'][$h],
'tmp_name' => $arr_files['tmp_name'][$h],
'error' => $arr_files['error'][$h],
'size' => $arr_files['size'][$h]);
And then run the loop like normal.
See previous related answer
Your code is very similar
to my answer at limiting the checking condition while uploading swf files
This is how you should implement such ..
FULL Script
<?php
error_reporting ( E_ALL );
$allowedExtensions = array (
'jpg',
'jpeg',
'png',
'bmp',
'tiff',
'gif'
);
$maxSize = 2097152;
$dirImage = "photos/tmp_images";
$errors = $output = array ();
if (isset ( $_FILES ['image'] )) {
foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {
$fileName = $_FILES ['image'] ['name'] [$key];
$fileSize = $_FILES ['image'] ['size'] [$key];
$fileTemp = $_FILES ['image'] ['tmp_name'] [$key];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
if (empty ( $fileName ))
continue;
// Dateiendung überprüfen
if (! in_array ( $fileExt, $allowedExtensions )) {
$errors [$fileName] [] = "format $fileExt in $fileName is not accepted";
}
if ($fileSize > $maxSize) {
$errors [$fileName] [] = "maxsize of 2MB exceeded";
}
if (! mkdir_recursive ( $dirImage, 0777 )) {
$errors [$fileName] [] = "Error Creating /Writing Directory $dirImage ";
}
// Construct destination path
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
$filePrifix = basename ( $fileName, "." . $fileExt );
$i = 0;
while ( file_exists ( $fileDst ) ) {
$i ++;
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
}
// Move the file
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
// ...
$output [$fileName] = "OK";
}
}
}
}
function mkdir_recursive($pathname, $mode) {
is_dir ( dirname ( $pathname ) ) || mkdir_recursive ( dirname ( $pathname ), $mode );
return is_dir ( $pathname ) || mkdir ( $pathname, $mode );
}
if (! empty ( $errors )) {
echo "<pre>";
foreach ( $errors as $file => $error ) {
echo $file, PHP_EOL;
echo "==============", PHP_EOL;
foreach ( $error as $line ) {
echo $line, PHP_EOL;
}
echo PHP_EOL;
}
echo "</pre>";
}
if (! empty ( $output )) {
echo "<pre>";
echo "Uploaded Files", PHP_EOL;
foreach ( $output as $file => $status ) {
echo $file, "=", $status, PHP_EOL;
}
echo "</pre>";
}
?>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename 1:</label> <input type="file" name="image[]"
id="file" /> <br /> <label for="file">Filename 2:</label> <input
type="file" name="image[]" id="file" /> <br /> <label for="file">Filename
3:</label> <input type="file" name="image[]" id="file" /> <br /> <input
type="submit" name="submit" value="Submit" />
</form>