我喜欢在数组的帮助下将一些图像上传到目录。因此我有这个代码:
$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)) {
...
}
}
该代码的问题如下:如果上传两个或多个具有可接受结尾的文件,它将回显:
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to access abc2.png in /a/b/xxx.php on line xxx
它指的是那条线:
if (move_uploaded_file($fileTmp, $fileDst)) {
除第一张图片外,每张图片都会显示此消息。所以我不知道我做错了什么。如果有人可以帮助我,我将不胜感激。我真的很感激。多谢。