我想上传多个文件而不只是一个。我知道如何上传单个文件(图片)我需要帮助上传多个文件。您可以在下面看到我如何上传单个文件。
添加时,这是我的HTML:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" multiple><br>
<input type="submit" name="submit" value="Submit">
</form>
我看到有些人将 name =“file”更改为 name =“file []”。但我不知道如何用php上传它。
这是PHP代码
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$random_digit=rand(0000,9999);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$file_name = $_FILES["file"]["name"];
$ext = pathinfo($file_name, PATHINFO_EXTENSION); // Get the extension of a file
$new_file_name = $random_digit . '.' . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_file_name);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
答案 0 :(得分:3)
如果您使用 name =“file []” $_FILES["file"]["name"]
将是一个数组(),您可以使用print_r($_FILES["file"])
或var_dump($_FILES["file"])
对您的数组进行测试看起来像这样:
Array
(
[uploads] => Array
(
[name] => Array
(
[0] => hack-attempt.sh
[1] => picture.gif
[2] => text.txt
[3] => virus.exe
)
[type] => Array
(
[0] => application/sh
[1] => image/jpeg
[2] => text/plain
[3] => applicatio/octet-stream
)
[tmp_name] => Array
(
[0] => /tmp/hack-attempt.sh
[1] => /tmp/picture.gif
[2] => /tmp/text.txt
[3] => /tmp/virus.exe
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[size] => Array
(
[0] => 217297
[1] => 53600
[2] => 6511924
[3] => 127662
)
)
)
示例来自:http://staticfloat.com/php-programmieren/multiupload-mit-html5-und-php/它是德国人,但仅仅是为了信用。
答案 1 :(得分:1)
您的上传表单需要有多个具有相同名称的输入字段。
我们称之为'upload []'
然后在你的接收脚本中,你需要遍历$ _FILES []数组才能到达每一个。
$response = "";
$i = 0;
while ((isset($_FILES['upload']['name'][$i])) && ($_FILES['upload']['name'][$i]<>"")){
$error = $_FILES['upload']['error'][$i];
if ($error == 0){
$upfilename = $_FILES['upload']['name'][$i];
//should probably validate your filename here
//$upfilename = validate_fname($upfilename)
$tmpname = $_FILES['upload']['tmp_name'][$i];
$dirname = "uploads" // your files to be stored in this directory
if (move_uploaded_file($tmpname, $dirname."/".$filename)){
$response .= "<br><div class=\"message\">File : $filename : uploaded successfully. Thank you.</div><br>";
}else {
$response .= "<br> <div class=\"message\"> There was an error uploading file: $filename ";
}
}
else $response = "<br><div class=\"errormsg\">there was an error - error code: $error</div><br>";
$i++;
}