Php 多个图像上载未在目录上移动文件
尝试构建一个多图像上载,从我的表单中获取3个提交的文件并将其存储在我的服务器上 我有以下几点,但无论我收到什么“无效文件”,任何人都能看出我哪里出了问题吗Php 多个图像上载未在目录上移动文件,php,mysql,pdo,Php,Mysql,Pdo,尝试构建一个多图像上载,从我的表单中获取3个提交的文件并将其存储在我的服务器上 我有以下几点,但无论我收到什么“无效文件”,任何人都能看出我哪里出了问题吗 for($i = 0; $i < 3; $i++) { $aFile = $_FILES['file'][$i]; $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $aFile["file"]["n
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";
}
}
($i=0;$i<3;$i++)的{
$aFile=$_文件['file'][$i];
$allowedExts=数组(“jpg”、“jpeg”、“gif”、“png”);
$extension=end(分解(“.”,$aFile[“file”][“name”]);
如果(($aFile[“file”][“type”]=“image/gif”)
||($aFile[“文件”][“类型”]=“图像/jpeg”)
||($aFile[“文件”][“类型”]=“图像/png”)
||($aFile[“文件”][“类型”]=“图像/pjpeg”))
&&($aFile[“文件”][“大小”]<20000)
&&in_数组($extension$allowedExts))
{
如果($aFile[“file”][“error”]>0)
{
回显“返回代码:”.$aFile[“文件”][“错误”]。“
”;
}
其他的
{
如果(文件_存在(“上载/”$aFile[“文件”][“名称”]))
{
echo$aFile[“文件”][“名称”]。“已存在。”;
}
其他的
{
移动上传的文件($aFile['tmp\u name'],
“upload/”.date('U')。“-”$aFile[“file”][“name”]);//向上载的文件名添加唯一字符串,使其唯一。
echo“图像上传成功”;
}
}
}
其他的
{
回显“无效文件”;
}
}
**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>
图1:
图2:
图3:
文件大小以字节表示
其中一个条件是文件大小限制检查:($aFile[“file”][“size”]<20000)
这将文件大小限制降低到20KB
你可能刚好超过了这个限制
检查您正在上载的文件的大小
更新
这是提交三个上载字段时文件结构的外观:
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
变量有问题。我试图运行您的脚本,但出现了错误(未定义索引等)。我想,主要问题是HTML元素名称与$\u文件中使用的名称无法匹配,即您使用了第一个文件输入名称“file”。它应该是“图片1”。您在文件处理部分使用了索引0到2。它应该是1到3以匹配“picture_1”、“picture_2”和“picture_3”
请注意,您的表单应具有enctype=“multipart/form data”
,否则您的文件将无法上载。这是正确的一个:
有两种方法可以实现这一点:
(1) 单独命名文件输入,如图片1、图片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>
图1:
图2:
图3:
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";
}
}
}
<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>
if(sizeof($\u文件)){
对于($i=1;$i 0)
{
回显“返回代码:”.$aFile[“错误”]。“
”;
}
其他的
{
如果(文件_存在(“upload/”$aFile[“name”]))
{
echo$aFile[“name”]”已存在。“;
}
其他的
{
移动上传的文件($aFile['tmp\u name'],
“upload/”.date('U')。“-”$aFile[“name”]);
echo“图像上传成功”;
}
}
}
其他的
{
回显“无效文件”;
}
}
}
方法2:将文件输入命名为一个组
HTML
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";
}
}
}
<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>
图1:
图2:
图3:
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";
}
}
}
if(sizeof($\u文件)){
对于($i=0;$i<3;$i++){
$name=$_文件['file']['name'][$i];
$type=$_文件['file']['type'][$i];
$tmp_name=$_FILES['file']['tmp_name'][$i];
$error=$_文件['file']['error'][$i];
$size=$_文件['file']['size'][$i];
if(empty($name))continue;#跳过空元素
$allowedExts=数组(“jpg”、“jpeg”、“gif”、“png”);
$extension=end(分解(“.”,$name));
如果(($type==“image/gif”)
||($type==“图像/jpeg”)
||($type==“图像/png”)
||($type==“图像/pjpeg”))
&&$size<20000
&&in_数组(strtolower($extension),$allowedExts))
{
如果($error>0)
{
回显“返回代码:.”错误。“
”;
}
其他的
{
如果(文件_存在(“上传/”$name))
{
echo$aFile[“文件”][“名称”]。“已存在。”;
}
其他的
{
移动上传的文件($tmp\U名称,
“upload/”.date('U')。“-”$name);
echo“图像上传成功”;
}
}
}
其他的
{
回显“无效文件”;
}
}
}
学分:
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";
}
}
}
<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>
- 应使用小写字母检查文件扩展名
. 李>
- 如果您使用
,您可以
在各自的HTML元素中具有相同的ID属性,例如,
。单击标签时,将触发元素的onclick事件
检查语句中的哪个布尔值是false
?调试101.u是否尝试先上载单个文件?显示HTML。你的html输入文件名可能有误。我尝试了一个文件,它可以在@arunu工作…检查这个链接。它类似于你的问题是,文件扩展名包含上一个字符