PHP 中的多个文件上传总是丢弃第一个文件
Multiple file upload in PHP always drops first file
我正在尝试 PHP 多个文件上传的不同示例,例如这个
<html>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<div>
<label for='upload'>Add files</label>
<input id='upload' name="upload[]" type="file" multiple="multiple" />
</div>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
//show success message
echo "<h1>Uploaded:</h1>";
if(is_array($files)){
echo "<ul>";
foreach($files as $file){
echo "<li>$file</li>";
}
echo "</ul>";
}
}
?>
问题是它检测到 3 个文件但只上传了 2 个并且第一个总是丢失。
我没有收到任何错误消息,所以无法理解发生了什么。我已经尝试了一些其他的多上传代码示例,但情况是一样的。
我读了这条评论:
https://www.php.net/manual/en/reserved.variables.files.php#89674
并且文件不是编号file0
、file1
、file2
等,而是file1
、file2
、file3
。可能是数字索引也不是 0
、1
、2
等,而是 1
、2
、3
等. 所以在你的情况下你可以通过做更正:
$tmpFilePath = $_FILES['upload']['tmp_name'][$i+1];
$shortname = $_FILES['upload']['name'][$i+1];
$filePath = $_FILES['upload']['name'][$i+1];
这个我没有测试过,我也不是很确定,但是你查起来很容易,做一个:
var_dump($_FILES);
我正在尝试 PHP 多个文件上传的不同示例,例如这个
<html>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<div>
<label for='upload'>Add files</label>
<input id='upload' name="upload[]" type="file" multiple="multiple" />
</div>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
//show success message
echo "<h1>Uploaded:</h1>";
if(is_array($files)){
echo "<ul>";
foreach($files as $file){
echo "<li>$file</li>";
}
echo "</ul>";
}
}
?>
问题是它检测到 3 个文件但只上传了 2 个并且第一个总是丢失。 我没有收到任何错误消息,所以无法理解发生了什么。我已经尝试了一些其他的多上传代码示例,但情况是一样的。
我读了这条评论:
https://www.php.net/manual/en/reserved.variables.files.php#89674
并且文件不是编号file0
、file1
、file2
等,而是file1
、file2
、file3
。可能是数字索引也不是 0
、1
、2
等,而是 1
、2
、3
等. 所以在你的情况下你可以通过做更正:
$tmpFilePath = $_FILES['upload']['tmp_name'][$i+1];
$shortname = $_FILES['upload']['name'][$i+1];
$filePath = $_FILES['upload']['name'][$i+1];
这个我没有测试过,我也不是很确定,但是你查起来很容易,做一个:
var_dump($_FILES);