Unspecified error with a file upload loop
I'm using a form and a loop to upload multiple image files directly to the file server, but I'm getting a false result with the move_uploaded_file function.
Upload Form:
<body>
<p>
<form action='uploadform.php' method='post' enctype='multipart/form-data'>
Select the files you would like to upload.
<input type='file' name='fileToUpload[]' id='fileToUpload' mozdirectory webkitdirectory directory multiple />
<input type='submit' value='Upload Image' name='submit'>
</form><br>
The files will be uploaded to a folder named '".$_SESSION['filename']."'.<br>
</p>
</body>
Multiple file uploading loop (uploadform.php:
if (isset($_POST["submit"])){
foreach ($_FILES['fileToUpload']['name'] as $i => $name) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"][$i]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (strlen($_FILES['fileToUpload']['name'][$i]) > 1) {
if (move_uploaded_file($_FILES["fileToUpload"]["name"][$i], $target_file)) {
echo "The file'". basename($_FILES["fileToUpload"]["name"][$i]). "' has been uploaded to the server.";
} else {
echo "<br>*** ERROR ***<br>";
echo "Current File basename: ".basename($_FILES["fileToUpload"]["name"][$i])."<br>";
echo "Current File non-basename: ".$_FILES["fileToUpload"]["name"][$i]."<br>";
echo "Current File Path: ".$target_file."<br>";
echo "Image file type: ".$imageFileType."<br>";
}
$count++;
}
}
}
When uploading one or multiple files with the form, it goes to the else statement echoing the "ERROR" string. The Apache Error Log comes up blank, so I have no clue what's wrong with the code.
I tried echoing the variables used in the loop ($_FILES["fileToUpload"]["name"][$i], $target_file and $imageFileType) but these seem to be fine.
我会把整个foreach循环放在try-catch块中,看看发生了什么异常:
try{
// your foreach loop here:
}
catch(Exception $e)
{
echo $e->getMessage();
}
The $_FILES["fileToUpload"]["name"][$i]
variable was not the one that the loop was supposed to use.
By changing all instances of $_FILES["fileToUpload"]["name"][$i]
to $name
(which is $_FILES["fileToUpload"]["tmp_name"][$i]
) the error was gone.
上一篇: 将视频上传到XAMPP
下一篇: 文件上传循环未指定的错误