尝试构建一个多图像上传,从我的表单中获取3个提交的文件并将它们存储在我的服务器上。
我有以下但是无论我收到什么“无效文件”可以anybdoy看到我出错的地方?
for($i = 0; $i < 3; $i++) {
$aFile = $_FILES['file'][$i];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["file"]["name"]));
if ((($aFile["file"]["type"] == "image/gif")
|| ($aFile["file"]["type"] == "image/jpeg")
|| ($aFile["file"]["type"] == "image/png")
|| ($aFile["file"]["type"] == "image/pjpeg"))
&& ($aFile["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($aFile["file"]["error"] > 0)
{
echo "Return Code: " .$aFile["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["file"]["name"]))
{
echo $aFile["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'],
"upload/" . date('U')."-".$aFile["file"]["name"]); // add a unique string to the uploaded filename so that it is unique.
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
** HTML **
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="file">
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="picture_2">
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="picture_3">
</li>
答案 0 :(得分:1)
我猜,主要问题是您的HTML元素名称和$_FILES
中使用的名称无法匹配,即您使用了第一个文件输入名称“file”。它应该是“picture_1”。并且您在文件处理部分使用了索引0到2。匹配“picture_1”,“picture_2”和“picture_3”应为1到3。
请注意,您的表单应为enctype="multipart/form-data"
,否则您的文件将无法上传。这是正确的:
有两种方法可以实现这一点:
(1)单独命名文件输入,如picture_1,picture_2等。
(2)将文件输入命名为file[]
等组。
方法1:单独命名文件输入
<强> HTML 强>
<form method="post" enctype="multipart/form-data">
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="picture_1" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="picture_2" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="picture_3" id="picture_3" />
</li>
<input type="submit" name="submit" value="Upload" />
</form>
PHP
if(sizeof($_FILES)){
for($i = 1; $i <= 3; $i++) {
$aFile = $_FILES['picture_'.$i];
if(empty($aFile['tmp_name'])) continue; # skip for empty elements
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["name"]));
if ((($aFile["type"] == "image/gif")
|| ($aFile["type"] == "image/jpeg")
|| ($aFile["type"] == "image/png")
|| ($aFile["type"] == "image/pjpeg"))
&& ($aFile["size"] < 20000)
&& in_array(strtolower($extension), $allowedExts))
{
if ($aFile["error"] > 0)
{
echo "Return Code: " .$aFile["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["name"]))
{
echo $aFile["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'],
"upload/" . date('U')."-".$aFile["name"]);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
方法2:将文件输入命名为一组
HTML
<form method="post" enctype="multipart/form-data">
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="file[]" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="file[]" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="file[]" id="picture_3" />
</li>
<input type="submit" name="submit" value="Upload" />
</form>
<强> PHP 强>
if(sizeof($_FILES)){
for($i = 0; $i < 3; $i++) {
$name = $_FILES['file']['name'][$i];
$type = $_FILES['file']['type'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
$error = $_FILES['file']['error'][$i];
$size = $_FILES['file']['size'][$i];
if(empty($name)) continue; # skip for empty element
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $name));
if (( ($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/png")
|| ($type == "image/pjpeg"))
&& $size < 20000
&& in_array(strtolower($extension), $allowedExts) )
{
if ($error > 0)
{
echo "Return Code: " .$error . "<br>";
}
else
{
if (file_exists("upload/" . $name))
{
echo $aFile["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($tmp_name,
"upload/" . date('U')."-".$name);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
CREDITS:
<label for="some_id">
,则可以
在您的相应HTML元素中具有相同的ID属性,例如<input
type="file" name="..." id="some_id" />
。单击标签时,将触发元素的 onclick 事件。答案 1 :(得分:0)
文件大小以字节为单位。
其中一个条件是文件大小限制检查:($aFile["file"]["size"] < 20000)
这会将文件大小限制降低到20 KB。
可能恰好超出了这个限制。
检查要上传的文件的大小。
<强>更新强>
这是提交三个上传字段时文件结构的样子:
array (size=1)
'file' =>
array (size=5)
'name' =>
array (size=3)
1 => string 'all_products (6).csv' (length=20)
2 => string 'pricero (3).csv' (length=15)
3 => string 'pricero.csv' (length=11)
'type' =>
array (size=3)
1 => string 'application/octet-stream' (length=24)
2 => string 'application/octet-stream' (length=24)
3 => string 'application/octet-stream' (length=24)
'tmp_name' =>
array (size=3)
1 => string 'E:\server\wamp\tmp\phpEF79.tmp' (length=30)
2 => string 'E:\server\wamp\tmp\phpEF7A.tmp' (length=30)
3 => string 'E:\server\wamp\tmp\phpEF7B.tmp' (length=30)
'error' =>
array (size=3)
1 => int 0
2 => int 0
3 => int 0
'size' =>
array (size=3)
1 => int 29702
2 => int 23095
3 => int 23095
您的$aFile
变量有问题。我试图运行你的脚本并得到错误(未定义的索引等)。