我想一次选择更多图像文件并上传为1,2,3,4,...赞:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgs[]" id="imgs" multiple/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP代码:
<?php
if (file_exists("upload/" . $_FILES["imgs"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
//I think loop goes here
===================
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
===================
}
?>
=============================================== ===================
这是有效的......
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploads[]" multiple>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
<?php
$count=1;
foreach ($_FILES['uploads']['tmp_name'] as $file) {
echo '<li>' . $file . '</li>';
copy($file, "uploads/" . $count.".jpg");
echo "Stored in: " . "uploads/" . $count.".jpg";
$count++;
}
?>
这是有效的....
答案 0 :(得分:3)
用以下内容替换您的PHP代码:
<?php
for($i=0;$i<count($_FILES["imgs"]["name"]);$i++)
{
if (file_exists("upload/" . $_FILES["imgs"]["name"][$i]))
{
echo $_FILES["imgs"]["name"][$i] . " already exists. ";
}
else
{
//I think loop goes here
===================
move_uploaded_file($_FILES["imgs"]["tmp_name"][$i],
"upload/" . $_FILES["imgs"]["name"][$i]);
echo "Stored in: " . "upload/" . $_FILES["imgs"]["name"][$i];
===================
}
}
?>
答案 1 :(得分:1)
当您有多个上传时,PHP会以奇怪的(但一致的)方式构建$_FILES
数组。尝试使用三个文件输入创建此表单:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgs[]" id="imgs" multiple />
<input type="submit" name="submit" value="Submit" />
</form>
然后在接收表单的PHP代码中,仅放置:
<?php print_r($_FILES); ?>
所以你可以看到$_FILES
数组结构。在这一点上,所有人都会清楚,你将自己如何循环以保存所有上传的图像。祝你好运;)
答案 2 :(得分:0)
我已经测试了这段代码,它有效,一旦我解决了,我忘了回答它。
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploads[]" multiple>
<input type="submit" name="submit" value="Submit" />
</form>
获取PHP文件中的数据,如下所示。
<?php
$count=1;
foreach ($_FILES['uploads']['tmp_name'] as $file) {
echo '<li>' . $file . '</li>';
copy($file, "uploads/" . $count.".jpg");
echo "Stored in: " . "uploads/" . $count.".jpg";
$count++;
}
?>