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 ...
随机推荐
- (转)TinyHttp源码剖析
tinyhttpd 是一个不到 500 行的超轻量型 Http Server,用来学习非常不错,可以帮助我们真正理解服务器程序的本质. 看完所有源码,真的感觉有很大收获,无论是 unix 的编程,还是 ...
- myeclipse便捷导包方式
1.将spring框架的core导成如图的方式 2.选中项目右键--bulid path--进入buildpath 3.选择add library 4.选择user library 5.选择user ...
- mysql 数据库查询
myspl 三部分 文件 服务 界面 查看数据库命令 show databases 创建数据库 create database 库名 删除数据库 drop database ...
- BZOJ 2038 小z的袜子(莫队)
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- Subsequence——POJ3061
题目:http://poj.org/problem?id=3061 尺取法解题 import java.util.Scanner;; public class Main { public static ...
- MVC4 Action 方法的执行
1. ActionInvoker 的执行: 在MVC 中 包括Model绑定与验证在内的整个Action的执行是通过一个名为ActionInvoker的组件来完成的. 它同样具有 同步/异步两个版本 ...
- 基于JSP+Servlet开发手机销售购物商城系统(前台+后台)源码
基于JSP+Servlet开发手机销售购物商城系统(前台+后台) 开发环境: Windows操作系统 开发工具:Eclipse/MyEclipse+Jdk+Tomcat+MySQL数据库 运行效果图: ...
- 关于获取客户端Mac地址
private static string GetClientMAC() { string mac_dest = string.Empty; try { string strClientIP = Ht ...
- 反射:获取Class对象的三种方式
获取Class对象的三种方式 package lianxiApril18; /** * 获取Class对象的三种方式 * 1 Object ——> getClass(); * 2 任何数据类型( ...
- Web Server 在IIS上部署ASP.NET Core项目
在IIS上部署ASP.NET Core项目 一.配置应用程序池为无托管: 二.安装ASPNETCoreModule:(核心) 下载地址:https://go.microsoft.com/fwlink/ ...