input file用了multiple属性,而且选择了多张图片,PHP该怎么上传?
展开全部
其实就那两个函数
is_uploaded_file()和move_uploaded_file()
是循环上传的。你打印下$_FILES这个超全局数组就明白了。下面我的测试代码上传图片的:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" id="f" name="images[]" multiple="true" style="border:1px solid red" />
<input type="submit" value="提交" />
</form>
</body>
</html>
<?php
$path=$_SERVER['DOCUMENT_ROOT'].'test2/';
if(!empty($_FILES)){
echo "aaaa";
if(is_uploaded_file($_FILES['images']['tmp_name'][0])){
echo "1111";
exit;
}
foreach($_FILES['images']['tmp_name'] as $k=>$v){
if(is_uploaded_file($_FILES['images']['tmp_name'][$k])){
$save=$path.$_FILES['images']['name'][$k];
echo $save."<br>";
if(move_uploaded_file($_FILES['images']['tmp_name'][$k],$save)){
echo "上传成功!";
}
}
}
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
?>
is_uploaded_file()和move_uploaded_file()
是循环上传的。你打印下$_FILES这个超全局数组就明白了。下面我的测试代码上传图片的:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" id="f" name="images[]" multiple="true" style="border:1px solid red" />
<input type="submit" value="提交" />
</form>
</body>
</html>
<?php
$path=$_SERVER['DOCUMENT_ROOT'].'test2/';
if(!empty($_FILES)){
echo "aaaa";
if(is_uploaded_file($_FILES['images']['tmp_name'][0])){
echo "1111";
exit;
}
foreach($_FILES['images']['tmp_name'] as $k=>$v){
if(is_uploaded_file($_FILES['images']['tmp_name'][$k])){
$save=$path.$_FILES['images']['name'][$k];
echo $save."<br>";
if(move_uploaded_file($_FILES['images']['tmp_name'][$k],$save)){
echo "上传成功!";
}
}
}
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询