我正在尝试使用一个表单帖子上传两个文件,但是我的表单只会上传第一个文件而不会迭代第二个文件并上传。知道为什么吗?我正在使用的代码如下。
<?php
$upload = $_POST['upload'];
$enteredPassword = $_POST['password'];
$uploadedFiles = $_FILES;
$password = "************";
// Where the file is going to be placed
$target_path = "";
//interger count
$i = 0;
//upload the files
if ($upload == true && $enteredPassword == $password) {
foreach($uploadedFiles as $uploadedFile) {
$target_path = $target_path . $uploadedFile['name'][$i];
if(move_uploaded_file($uploadedFile['tmp_name'][$i], $target_path)) {
echo "<p>The file ". $uploadedFile['name'][$i].
" has been uploaded.</p>";
} else{
echo "<p>There was an error uploading the {$uploadedfile['name']}, please try again!</p>";
}
$i++;
}
}
?>
<ul>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="upload" value="true" />
<li>Choose a file to upload:</li>
<li><input name="userfile[]" type="file" size="40" /></li>
<li><input name="userfile[]" type="file" size="40" /></li>
<li>Enter password for file upload:</li>
<li><input name="password" type="password" size="40" /></li>
<li><input type="submit" value="Upload File" /></li>
</form>
</ul>
答案 0 :(得分:0)
如果您愿意,可以使用此插件进行多个图片上传。它的作品对我很有用...... http://www.plupload.com/example_queuewidget.php
答案 1 :(得分:0)
尝试,uploadify 它适用于具有出色api和回调的多个上传。 http://www.uploadify.com/
答案 2 :(得分:0)
行为是预期的,因为$ _FILES数组中只有一个元素是$ _FILES [“userfile”]。您可以尝试这样的方法来实现您的目标:
<?php
if(isset($_POST["submit"]))
{
$upload = $_POST['upload'];
$enteredPassword = $_POST['password'];
$uploadedFiles = $_FILES["userfile"];
$password = "12345";
// Where the file is going to be placed
$base_path = "upload/";
//upload the files
if ($upload == true && $enteredPassword == $password) {
foreach(array_combine($uploadedFiles["name"], $uploadedFiles["tmp_name"]) as $name => $tmp_name) {
$target_path = $base_path .$name;
if(move_uploaded_file($tmp_name, $target_path)) {
echo "<p>The file ". $name." has been uploaded.</p>";
}
else
{
echo "<p>There was an error uploading the {$name}, please try again!</p>";
}
}
}
}
?>
<ul>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="upload" value="true" />
<li>Choose a file to upload:</li>
<li><input name="userfile[]" type="file" /></li>
<li><input name="userfile[]" type="file" /></li>
<li>Enter password for file upload:</li>
<li><input name="password" type="password" size="40" /></li>
<li><input type="submit" name="submit" value="Upload File" /></li>
</form>
</ul>