我正在尝试将多个文件上传到我的服务器。如果我尝试上传单个文件,它可以正常工作。但是如果我尝试多个,它会给我一个错误代码4,即使它确实正确地打印了所有文件的名称。没有上传任何内容。我正确设置了输入类型。有人能帮我吗?
Choose Image: <input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br />
<input type="submit" value="Upload Image!" style="margin-left:100px;"/>
以下是代码:
$i=0;
foreach($_FILES['uploadedfile']['name'] as $f)
{
$file['name'] = $_FILES['uploadedfile']['name'][$i];
$file['type'] = $_FILES['uploadedfile']['type'][$i];
$file['tmp_name'] = $_FILES['uploadedfile']['tmp_name'][$i];
$file['error'] = $_FILES['uploadedfile']['error'][$i];
$file['size'] = $_FILES['uploadedfile']['size'][$i];
if ($file["error"] > 0)
{
echo "Error Code: " . $file["error"];
}
$target_path = "uploads/".basename($file["name"]);
if(move_uploaded_file($file["tmp_name"], $target_path))
{
echo basename($file['name'])."<br />";
echo basename($file['tmp_name'])."<br />";
echo $target_path;
} else{
echo "There was an error uploading the file, please try again!";
}
$i++;
}
和我的HTML表单
<div id="album_slider">
<div style="text-align:center;margin:20px auto;font-size:27px;">Upload Image</div>
<br style="clear:both;font-size:0;line-height:0;height:0;"/>
<div style="width:700px;margin:auto;height:250px;text-align:left;">
<form enctype="multipart/form-data" action="uploader.php" method="POST" name="form">
Image Name: <input type="text" name="image_name" id="image_name"/><br /><br /><br />
<input type="hidden" name="a_id" id="a_id" value="<?php echo $a_id; ?>"/>
Choose Image: <input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br />
<input type="submit" value="Upload Image!" style="margin-left:100px;"/>
</form>
</div>
<br style="clear:both;font-size:0;line-height:0;height:1px;"/>
</div>
答案 0 :(得分:6)
尝试:
foreach ($_FILES["uploadedfile"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { echo "$error_codes[$error]"; move_uploaded_file( $_FILES["uploadedfile"]["tmp_name"][$key], $target_path ) or die("Problems with upload"); } }
答案 1 :(得分:3)
因为您要上传 MULTIPLE 文件,它会失败,但您的代码只能处理1个单个文件。
请继续阅读如何处理多个文件:
http://php.net/manual/en/features.file-upload.multiple.php
答案 2 :(得分:0)
像下面的代码一样使用条件检查
if(isset($_FILES["qImage"]) && !empty($_FILES["qImage"]["name"])){
$imgSubQuestion = $_FILES["qImage"];
}
if(isset($_FILES["sImage"]) && !empty($_FILES["sImage"]["name"])){
$imgSolution = $_FILES["sImage"];
}