php 图片上传 并返回上传文件位置 支持多文件上传
<?php
/**
* Created by PhpStorm.
* User: DY040
* Date: 2018/4/26
* Time: 13:23
*/
echo '<pre>'; /*
* 支持多文件上传 检查所有的文件 是否符合要求 储存于 服务器 并返回图片地址
* */ class ImgUpload1
{
protected static $imgs_file = array();//要返回的数据 (储存的文件信息) function __construct($files)
{ foreach ($files as $k => $v) {
$img_info = $files[$k];
$arr = array();
if (gettype($img_info['name']) === 'array') { foreach ($img_info['name'] as $key => $value) {
$arr['name'] = $img_info['name'][$key];
$arr['type'] = $img_info['type'][$key];
$arr['tmp_name'] = $img_info['tmp_name'][$key];
$arr['error'] = $img_info['error'][$key];
$arr['size'] = $img_info['size'][$key];
$this->_check($arr, $k);
}
} else {
$this->_check($img_info, $k);
} } } protected function _check($img_info, $index)
{ if (!isset(static::$imgs_file[$index])) { static::$imgs_file[$index] = $this->_img_store($img_info) . "*";
} else {
static::$imgs_file[$index] .= $this->_img_store($img_info) . "*";
} } protected function _img_store($img_info)
{ //检测文件是否合法 ---文件名后缀
$img_hzm = array('.gif', '.png', '.jpg', '.jpeg');
$re = in_array(strrchr($img_info['name'], '.'), $img_hzm);
if (!$re) {
return '该图片不合法-文件名后缀';
}
//检测文件是否合法 ---MIME php检测上传信息的mime
if (explode('/', $img_info['type'])[0] !== 'image') {
return '该图片不合法-MIME';
}
//检测文件是否合法 ---MIME php检测本地的临时文件
// 这里要修改php.ini 增加php_fileinfo.dll模块
$file_info = new Finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->file($img_info['tmp_name']);
if (explode('/', $mime_type)[0] !== 'image') {
return '该图片不合法-MIME';
}
//检测文件大小 设为1mb
if ($img_info['size'] > 1000000) {
return '该图片不合法-文件大于1000kb';
}
//是否是http上传的临时文件
if (!is_uploaded_file($img_info['tmp_name'])) {
echo '文件名不合法';
return false;
}; //设置上传文件地址
if (!is_dir('.\upload')) {
mkdir('.\upload');
} $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'upload\\' . date('Ymdh');//按小时划分目录
$imgType = strrchr($img_info['name'], '.');//文件名后缀 $re = is_dir($path);
if (!$re) {
mkdir($path);
} $imgName = uniqid(rand(), true) . $imgType;
$img_file = $path . DIRECTORY_SEPARATOR . $imgName; $upload_re = move_uploaded_file($img_info['tmp_name'], $img_file);
if ($upload_re) { return $img_file; } else { return '文件上传失败'; } } public function imgs_file()
{
$arr = static::$imgs_file;
foreach ($arr as $k => $v) { if(gettype($_FILES[$k]['name'])==='string'){
$arr[$k] = substr($v, 0, -1);
}else{
$arr[$k] = explode('*', substr($v, 0, -1));
}
}
return $arr;
} } $img_upload = new ImgUpload1($_FILES);
//获取文件所在位置
print_r($img_upload->imgs_file());
/*
*
* Array
(
[user_img] => D:\PHP\www\hxq\day22\upload\2018042604\183775ae1878615f2e6.81933117.jpg
[pro_img] => Array
(
[0] => D:\PHP\www\hxq\day22\upload\2018042604\324625ae18786172b65.94418381.jpg
[1] => D:\PHP\www\hxq\day22\upload\2018042604\291675ae187861863e5.14296700.jpg
) )
*
* */
/*
* 对应表单
*
* <form action="imgUpload.class.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="user_img"></p> <p><input type="file" name="pro_img[]"></p> <p><input type="file" name="pro_img[]"></p> <p><input type="submit"></p> </form>
* */
php 图片上传 并返回上传文件位置 支持多文件上传的更多相关文章
- formdata方式上传文件,支持大文件分割上传
1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...
- LINUX 上源代码安装与配置samba服务,支持从windows上读写LINUX文件。
###动机###在windows编写代码文件比较方便,因为有source insight.但是需要在LINUX上编译.一种办法就是使用samba文件共享. [1] 下载samba代码.按照config ...
- lumen 支持多文件上传
1.webform (注意:name后面一定要加[]号) <form method="post" enctype="multipart/form-data" ...
- HTML上传文件支持大文件上传,下载
上传 1.修改配置文件web.config,在<system.webServer>下面加入 <security> <requestFiltering > <r ...
- PHP 图片上传工具类(支持多文件上传)
====================ImageUploadTool======================== <?php class ImageUploadTool { private ...
- ajax 提交所有表单内容及上传图片(文件),以及单独上传某个图片(文件)
我以演示上传图片为例子: java代码如下(前端童鞋可以直接跳过看下面的html及js): package com.vatuu.web.action; import java.io.File; imp ...
- springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传
总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...
- OAF_文件系列4_实现OAF上传显示数据库动态图片Image(案例)
20150805 Created By BaoXinjian
- ExtJs 4 的filefield上传后 返回值success接受不正常
问题解决了,我修改了返回类型为setContentType("text/html")可以正确解析了,感到很奇怪,其他的地方使用setContentType("applic ...
随机推荐
- 白盒测试实践--Day5
累计完成任务情况: 阶段内容 参与人 完成个人情况说明并提交作业 全体 汇总作业,查漏补缺,完成代码测试总结 小靳.小龙 完成测试小结 小黄.小尹 完成静态代码检查结果报告 小靳 完成JUnit脚本编 ...
- Windows 配置 nginx php 多版本切换
1. 下载 nginx nginx.org 2. 下载 php windows.php.net 选择 nts 版本,解压后,将php.ini.development 重命名为 php.in ...
- java 集合综述(总结于多位博友)
http://www.cnblogs.com/shunran/p/3459065.html(good) java集合类主要负责保存.盛装其他数据,因此集合类也称容器类. java集合类分为:set.l ...
- URAL 1698. Square Country 5(记忆化搜索)
题目链接 题意 : 自守数的定义:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数.例如5*5=25,则5就是自守数.让你求不超过n位的自守数有多少 思路 : 实际上,自守数还有两个性质 ...
- Locality preserving hashing for fast image search: theory and applications
Is there any Java library that provides an implementation (or several) of a Locality Preserving Hash ...
- [数学趣味001]RSA算法原理及示例
可以先看看这个视频: RSA_Encryption_Algorithm 公开密钥 Perwork: 私钥:Sender和Receiver预先约定加密和解密方案,向其他人保密. 这个实现比较难:向其他人 ...
- 三张图片看懂ZKEACMS的设计思想
前言 如果你还不知道ZKEACMS,不妨先了解一下. ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来 官方地址:http://www.zkea.net/zkeacms ...
- JS设置localStorage有效期
localStorage是一个没有时间限制的数据存储,如果没有手动删除它,它将永久保存. 但是有些时候我们又需要它在一段时间后自动删除,这里我们可以对它进行扩展. var date = new Dat ...
- Oracle彻底卸载
Oracle彻底卸载 卸载:oracle卸载1.删除注册表:打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> HKEY_LOCAL_MAC ...
- MongoDB高级知识-易扩展
MongoDB高级知识-易扩展 应用程序数据集的大小正在以不可思议的速度增长.随着可用宽带的增长和存储器价格的下跌,即使是一个小规模的应用程序,需要存储的数据也可能大的惊人,甚至超出了很多数据库的处理 ...