单文件上传实现:
多文件上传实现:
前台调用:
- shell方式
curl http://127.0.0.1:8080/picture
-F "file1=@/Users/shijianmei/Downloads/hyd.png" -F "file2=@/Users/shijianmei/Downloads/erweima3.png" -v
- 浏览器方式:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="test1.php" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file1" id="file"><br>
<input type="file" name="file2" id="file"><br>
<input type="text" name="version" id="version"><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
后台处理:
<?php
$multi_info=$_FILES;
foreach($multi_info as $name=>$up_info){ //foreach 遍历$multi_info
uploadFile($up_info); //调用单文件上传函数
}
function uploadFile($up_info){
echo "字段version:" . $_POST["version"] . "<br>";
echo "上传文件名: " . $up_info["name"] . "<br>";
echo "文件类型: " . $up_info["type"] . "<br>";
echo "文件大小: " . ($up_info["size"] / 1024) . " kB<br>";
echo "文件临时存储的位置: " . $up_info["tmp_name"] . "<br>";
// 允许上传的图片后缀
$allowedExts = array("zip", "ipa", "jpg", "png");
$temp = explode(".", $up_info["name"]);
echo $up_info["size"];
$extension = end($temp); // 获取文件后缀名
if(!in_array($extension, $allowedExts)){
echo "文件格式为:" . $extension;
//exit ("非法的文件格式");
}
if($up_info["size"] > 204800000){
exit("上传文件超出限制大小");
}
if ($up_info["error"] > 0){
exit("错误:: " . $up_info["error"] . "<br>");
}
// 判断当期目录下的 upload 目录是否存在该文件,如果没有 upload 目录,你需要创建它,upload 目录权限为 777
if (file_exists("upload/" . $up_info["name"])){
exit ( $up_info["name"] . " 文件已经存在。 ");
}
// 上传文件
if(move_uploaded_file($up_info["tmp_name"], "upload/" . $up_info["name"]))
{
echo "文件存储在: " . "upload/" . $up_info["name"];
}
else{
echo "文件上传失败";
}
}
?>
遇到的问题:
1.文件上传到服务器文件夹下失败:原因,文件夹没有读写权限
chmod 777 /images/small
2.上传大文件失败:
- 修改php文件大小限制
max_execution_time = 150
max_input_time = 300
memory_limit = 256M
post_max_size = 20M
upload_max_filesize = 10M
- 修改apache上传文件大小限制
vi /etc/httpd/conf.d/php.conf
LimitRequestBody 524288将524288(=512x1024)改大,比如5M(=5x1024x1024)
注意配置完后需要重启。