while ループを使用して複数のファイルをアップロードしています。コードは次のとおりです。move_uploaded_file は常に失敗し、「ファイルのアップロード中にエラーが発生しました!」というメッセージが表示されます。私は同じコード ($counter なしの while ループ内のコード) を使用して、他のユースケースで単一のファイルをアップロードしていますが、これはうまく機能しますが、while ループで同じコードを使用すると、コードは問題ないように見えますが、で失敗しmove_uploaded_file
ます。 ..何が問題なのか、どうすれば解決できるのか教えてください。
$counter = 0;
while ($_FILES) {
if (!isset($_FILES['album_pics'])) {
//required variables are empty
die("File is empty!");
}
if ($_FILES['album_pics']['error'][$counter]) {
//File upload error encountered
die(upload_errors($_FILES['album_pics']['error'][$counter]));
}
$FileName = strtolower($_FILES['album_pics']['name'][$counter]); //uploaded file name
$ImageExt = substr($FileName, strrpos($FileName, '.')); //file extension
$FileType = $_FILES['album_pics']['type'][$counter]; //file type
$FileSize = $_FILES['album_pics']["size"][$counter]; //file size
$RandNumber = rand(0, 9999999999); //Random number to make each filename unique.
$uploaded_date = date("Y-m-d H:i:s");
$FileTitle = current(explode('.', $FileName));
switch (strtolower($FileType)) {
case 'image/png':
case 'image/gif':
case 'image/jpeg':
case 'application/pdf':
case 'application/msword':
case 'application/vnd.ms-excel':
case 'application/x-zip-compressed':
case 'text/plain':
case 'text/html':
break;
default:
die('Unsupported File!'); //output error
}
//File Title will be used as new File name
$NewFileName = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), strtolower(substr($FileTitle, 0, 10)));
$NewFileName = $NewFileName.'_'.$RandNumber.$ImageExt;
$UploadDirectory = 'ajax/';
if (move_uploaded_file($_FILES['album_pics']['tmp_name'][$counter], $UploadDirectory.$NewFileName)) {
// here I will insert image metadata to database with the new image name
$counter = (if result is true) ? $counter + 1 : $counter;// increase counter if insert returns true
} else {
die('error uploading File!');
}
function upload_errors($err_code) {
switch ($err_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'.ini_get("upload_max_filesize");
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
default:
return 'Unknown upload error';
}
} //function upload_erros() close
} //while loop close