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', 'tiff', 'gif');
$maxSize = 2097152;
$Dir = "a/b/c/d";
$storageDir = "a/b/c/d/tmp_images";
//Arrays
$errors2 = $output = array();
if(!empty($_FILES['image'])){
// Validation loop (I prefer for loops for this specific task)
for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) {
$fileName = $_FILES['image']['name'][$i];
$fileSize = $_FILES['image']['size'][$i];
/*$fileErr = $_FILES['image']['error'][$i];*/
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
// Dateiendung überprüfen
if (!in_array($fileExt, $allowedExtensions)) {
$errors2[$fileName][] = "format $fileExt in $fileName is not accepted";
}
// check filesize
if ($fileSize > $maxSize) {
$errors2[$fileName][] = "maxsize of 2MB exceeded";
}
}
/*// Handle validation errors here
if (count($errors) > 0) {
echo "Fehler beim Upload des Bildmaterials:";
echo ($errors); ->gibt "Array" aus
}*/
if (is_dir($Dir)){
mkdir($storageDir, 0755);
}else{
mkdir($Dir, 0755);
mkdir($storageDir, 0755);
}
// Fileupload
for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) {
// Get base info
$fileBase = basename($_FILES['image']['name'][$i]);
$fileName = pathinfo($fileBase, PATHINFO_FILENAME);
$fileExt = pathinfo($fileBase, PATHINFO_EXTENSION);
$fileTmp = $_FILES['image']['tmp_name'][$i];
// Construct destination path
$fileDst = $storageDir.'/'.basename($_FILES['image']['name'][$i]);
for ($j = 0; file_exists($fileDst); $j++) {
$fileDst = "$storageDir/$fileName-$j.$fileExt";
}
// Move the file
if (count($errors2) == 0) {
if (move_uploaded_file($fileTmp, $fileDst)) {
...
}
}
the problem with that code is the following: in case of uploading two or more files with an accepted ending it will echo out:
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to access abc2.png in /a/b/xxx.php on line xxx
which refers to that line:
if (move_uploaded_file($fileTmp, $fileDst)) {
this message will be shown for each picture except the first one. so i have no clue what i do wrong. i would really appreciate if there is someone who could help me out. i really would appreciate. thanks a lot.
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>
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?
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', ... ]
}
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