I want to upload two different files with different input file options. I think I am using the standard method in order to upload files and copy to different folder. Once files are uploaded it should be in /temp
folder from where it can be copied to different folder. With the following code I can only look for one file in temp and it is successfully copied to the desired foler. The file which was able to copied was the second upload file. However, there is no temp file created for the first upload file. And PHP is not giving any error. Any sugestion? I am unable to identify what is the problem in the following code:
我想上传两个具有不同输入文件选项的不同文件。我想我正在使用标准方法上传文件并复制到不同的文件夹。上传文件后,它应该位于/ temp文件夹中,可以从中复制到不同的文件夹。使用以下代码,我只能在temp中查找一个文件,并且它已成功复制到所需的foler。能够复制的文件是第二个上传文件。但是,没有为第一个上载文件创建临时文件。 PHP没有给出任何错误。任何消化?我无法确定以下代码中的问题:
<form>
Select File:
<input type="file" name="file[]" id= "file[]" maxlength="90" />
Select file2:
<input type="file" name="file[]" id= "file[]" maxlength="90"/>
<input type="submit" name = "submit" value="Submit"/>
<?php
if(isset($_FILES["file"])) {
print_r( $_FILES );
$target_dir = "upload/";
echo '<br>';
$no_files = count($_FILES["file"]['name']);
for ($i = 0; $i < $no_files; $i++) {
if ($_FILES["file"]["error"][$i] > 0) {
echo '<br>'. $i;
echo "Error: " . $_FILES["file"]["name"][$i] . "<br>";
}else {
$temp = explode(".", $_FILES["file"]["name"][$i]);
$extension = end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"][$i], $target_dir . $_FILES["file"]["name"][$i]);
echo "<br><font color='green'>".$_FILES["file"]["name"][$i] . " Uploaded Successfully.</font><br>";
}
}
}
?>
</form>
1 个解决方案
#1
It seems that your HTML form ist wrong and you don't use array form name.
您的HTML表单似乎错误,并且您不使用数组表单名称。
From PHP Manual:
来自PHP手册:
html client side:
html客户端:
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
And on server side:
在服务器端:
<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
See more on PHP.net
在PHP.net上查看更多
#1
It seems that your HTML form ist wrong and you don't use array form name.
您的HTML表单似乎错误,并且您不使用数组表单名称。
From PHP Manual:
来自PHP手册:
html client side:
html客户端:
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
And on server side:
在服务器端:
<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
See more on PHP.net
在PHP.net上查看更多