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 ...
随机推荐
- PCA 原理
PCA的数学原理(转) 1 年前 PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取 ...
- C#6.0特性(快来围观)(转)
出处:http://www.cnblogs.com/HJL-Blog/p/4457632.html 说明一下,很多博友一进来就认为仅仅是语法糖,C#语法的更新,代表着它的进步,语法糖是为了让我们更好的 ...
- Linux上获取CPU Core个数的实现
Linux上获取CPU Core个数的实现 可以通过多种手段取得CPU Core的个数,如: 1) 调用系统提供的函数get_nprocs(),可以在头文件sys/sysinfo.h中发现它 2) 借 ...
- 第08章 ElasticSearch Java API
本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...
- SSH整合案例
1.Hibernate框架 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernat ...
- js对象 1字符串对象2时间日期对象3数字对象
1字符串对象 直接对字符操作 var str = "这,是,不,是,字,符,串"; //字符串转数组 字符串.split(分隔符) var arr ...
- 把windows电脑变成路由器使用
实用小技巧1 把windows电脑变成路由器使用 适用对象: windows7.windows8的笔记本电脑或者有无线网卡的台式电脑 网络要求: CMCC-EDU和家里拨号上网的都可以,但是电信的校园 ...
- Windows server 2008 R2安装MySQL 32位ODBC驱动!
在Windows server 2008 R2安装MySQL 32位ODBC驱动,总是提示错误,我安装了DOTNET4的库,同时安装了VC2008.VC2012.VC2013的支持库,怎么还不行呢?M ...
- is 与 == 的区别;小数据池; 编码与解码
1, is 与 == 的区别 == 比较的是两边的值 is 比较的是两边的地址 id () 2,小数据池(在终端中) 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址 ...
- [ActionSprit 3.0] FMS服务器带宽检测
package { import flash.display.Sprite; import flash.net.NetConnection; import flash.events.NetStatus ...