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 ...
随机推荐
- 如何让两个div并排显示
正常情况下两个div都是上下排版的,那么怎么让它们并排显示呢? 方法一:都左浮动float:left; 方法二:一个左浮动,一个右浮动 方法三:给两个div都设置display:inline属性,但两 ...
- CodeForces 499D. Name That Tune(概率dp)
It turns out that you are a great fan of rock band AC/PE. Peter learned that and started the followi ...
- 洛谷P2387 [NOI2014]魔法森林(lct维护最小生成树)
题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,…,n,边标号为 1,2,3,…, ...
- CentOS 7.2安装zabbix 3.0 LTS
1.zabbix简介 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供 ...
- 解决VS2010中工具箱里没有WPM
我前段时间要用到MS的WPM,但苦于找不到解决不了,无奈重装后一样的情况我的win7 旗舰版版+VS2010旗舰版.在控制面板里找到多媒体重新关闭并从新打开,就OK啦..记得要重启哦...
- Replication--使用MSlogreader_history查看日志读起的延迟和事务命令
--======================================================================== MSlogreader_history 表存放本地 ...
- Partition--分区切换
现有数据表[dbo].[staging_TB1_20131018-104722]和分区表[dbo].[TB1],需要将分区表和数据表中做数据交换 CREATE TABLE [dbo].[staging ...
- HBase介绍(4)---常用shell命令
进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成功之 ...
- 关于.NET C#调用Sqlite的总结二
关于.NET C#调用Sqlite的总结一 在上一篇中我一直疑惑为什么我在使用多层架构进行开发时总是会报些莫名的错误,难道要使用Sqlite就不能分层吗?只能将UI.业务逻辑.数据访问统统都要写在一层 ...
- Windows上传文件到linux 使用winscp
Windows上传文件到linux 使用winscp, winscp下载目录 https://sourceforge.net/projects/winscp/postdownload?source=d ...