PHP 实现一种多文件上传的方法
作者:mrr 时间:2024-05-03 15:07:11
之前在实现表单中file类型input选择多图片的时候找到一种方式 也许不是最好的但亲测可行且支持ie7以上以及chrome浏览器
在表单中使用正常多文件选择multiple属性
<input type="file" id="image" class="file image hidden" name="image[]" multiple="true">
然后使用AjaxFileUpload或其他方式提交
将对应命名的file文件 $file[‘image']
转化为 json打印
正常格式
{"name":"7332.png","type":"image\/png","tmp_name":"\/tmp\/phplqppvR","error":0,"size":659}
但是此时结果为
{"name":["7656.png","7718.png"],"type":["image/png","image/png"],"tmp_name":["/tmp/phpDzSovj","/tmp/phpP8kWmT"],"error":[0,0],"size":[357,662]}
所有的属性都变为数组 按序排列
这时候可以使用以下代码实现图片保存
if (!isset($_FILES[$field])) {
return new JsonResponse(array('errorCode'=>1, 'message'=>'请上传文件'));
}
//重新命名$_FILE 存储多个文件上传
$arrayFile = array();
foreach($_FILES[$field] as $key => $value){
$i = 0;
if(is_array($value)) {
foreach ($value as $v) {
$i++;
//重命名后重新放入超全局变量_FILE 保证键名唯一 也可直接上传
$name = $field . '_split_' . $i;
$_FILES[$name][$key] = $v;
}
}
}
//是否上传多文件
if($i > 0){
for($j = 1; $j <= $i; $j++){ array_push($arrayFile, $field . '_split_' . $j); } }else{ array_push($arrayFile, $field); } //遍历file多个文件 上传 foreach($arrayFile as $file){ if (isset($_FILES[$file]) && $_FILES[$file]['name']) { //自定义上传方法 具体内容略 $data = $this->uploadFile($file, $path, uniqid());
if ( isset($data) && !empty($data) ) {
if(!isset($data['errors'])){
//将上传结果存储于$result中 多图片地址使用逗号拼接
if(isset($result)){
$result = array('errorCode'=>0, 'message'=>$result['message'] . ',' . reset($data));
}else{
$result = array('errorCode'=>0, 'message'=>reset($data));
}
}else{
//以下为返回错误信息
if(is_array(reset($data))){
$message = reset($data)[0];
}else{
$message = reset($data);
}
$result = array('errorCode' => 1, 'message' => $message);
}
} else {
$result = array('errorCode'=>1, 'message'=>'上传失败');
break;
}
} else {
$result = array('errorCode'=>1, 'message'=>'请上传文件');
break;
}
}
//返回上传结果
return $result;
总结
以上所述是小编给大家介绍的PHP 实现一种多文件上传的方法网站的支持!
来源:http://blog.it985.com/22328.html?utm_source=tuicool&utm_medium=referral
标签:php,多文件,上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言Http Server框架实现一个简单的httpServer
2024-02-19 11:04:33
![](https://img.aspxhome.com/file/2023/6/109726_0s.jpg)
将自己的数据集制作成TFRecord格式教程
2022-02-01 14:49:37
![](https://img.aspxhome.com/file/2023/0/103720_0s.jpg)
对python中的os.getpid()和os.fork()函数详解
2022-11-29 05:22:51
Python2.7编程中SQLite3基本操作方法示例
2023-11-14 04:13:32
Python模拟登录验证码(代码简单)
2022-09-04 04:41:37
这些关于Go中interface{}的注意事项你都了解吗
2024-02-01 08:20:19
Go 中 time.After 可能导致的内存泄露问题解析
2024-02-03 13:05:16
![](https://img.aspxhome.com/file/2023/7/108097_0s.png)
php使用socket简单实现通信功能
2023-05-31 13:01:23
![](https://img.aspxhome.com/file/2023/5/55295_0s.png)
基于mysq字段选择的详解
2024-01-23 20:11:20
javascript分页代码实例分享(js分页)
2023-10-11 10:00:57
python 自定义异常和主动抛出异常(raise)的操作
2022-03-22 12:43:22
![](https://img.aspxhome.com/file/2023/8/92148_0s.jpg)
为JavaScript程序添加客户端不可见的注释
2008-05-31 08:02:00
python 执行shell命令并将结果保存的实例
2023-07-27 20:34:52
一文彻底理解JS回调函数
2024-04-10 10:52:10
![](https://img.aspxhome.com/file/2023/9/136889_0s.png)
Macbook安装Python最新版本、GUI开发环境、图像处理、视频处理环境详解
2021-11-16 17:07:04
![](https://img.aspxhome.com/file/2023/5/112755_0s.png)
python import模块时有错误红线的原因
2021-03-19 13:59:30
![](https://img.aspxhome.com/file/2023/2/109412_0s.png)
Python实现的多线程http压力测试代码
2021-06-30 23:47:29
通过 for 循环比较 Python 与 Ruby 的编程区别
2022-11-12 01:19:26
python zip,lambda,map函数代码实例
2023-08-25 05:04:49
selenium中get_cookies()和add_cookie()的用法详解
2023-10-22 14:27:19