我想一次上传多个文件。我已经成功测试了单个文件上传的代码(删除了每个循环)然后它全部工作。文件正在上传并检查文件大小/文件类型。但是当我尝试输入for循环以便能够执行多个文件时,它返回" 不允许使用此文件扩展名,请上传JPEG或PNG文件 "
HTML代码:
<form action="fileUpload.php" method="POST" enctype="multipart/form-data">
<table>
<tr><td>File 1</td> <td><input name="file_upload[]" type="file" class="multi"/> </td></tr>
<tr><td>File 2</td> <td><input name="file_upload[]" type="file" class="multi"/> </td></tr>
<tr><td></td> <td> <input type="submit" name="upload" value="Upload"><input type="reset"> </td> </tr>
</table>
</form>
PHP代码:
foreach ($_FILES['file_upload']['tmp_name'] as $key => $tmp_name)
{
$rootDir = getcwd(); // get current working directory
$uploadDirectory = "/uploads/";
$fileExtensions = ['jpeg','jpg','png', 'pdf', 'docx',]; // Get all the file extensions
$fileType = $_FILES['file_upload']['type'][$key];
// $fileExtension = strtolower(end(explode('.',$fileName)));
$fileExtension = pathinfo($_FILES["file_upload"]["tmp_name"][$key]);
$uploadPath = $rootDir . $uploadDirectory . basename($fileName);
// echo $uploadPath;
}
?>
答案 0 :(得分:0)
更改:
$fileExtension = pathinfo($_FILES["file_upload"]["tmp_name"][$key]);
到:
$fileExtension = pathinfo($_FILES["file_upload"]["name"][$key]);
$fileExtension = $fileExtension['extension'];
以前,您的代码会检查临时文件扩展名。