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 ...
随机推荐
- Visual assint x(转)
最新破解版:Visual Assist X 10.8.2001 破解版 支持VC2013及以下版本 安装步骤: 1. 双击运行VA_A_Setup2001并安装 2. 双击运行Visual Assis ...
- 数据库工具链接阿里云MySQL数据库
数据库工具:Toad for MySQL ssh工具:XShell 5 跳板机配置: 配置通道: 源主机:数据库工具链接的地址: 侦听接口:数据库工具侦听接口: 目标主机:数据库阿里云地址: 目标端口 ...
- Sweetkang 的机器学习实验室 (1)
前言 近年来,Machine Learning 在许多领域上已然取得了可喜的成就,非常火热.就我个人来讲,有意将业余 Sport Programming 的范围扩展一下,譬如 Topcoder Mar ...
- [LeetCode 题解]: LetterCombinations
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- nginx uwsgi flask相关配置
一.安装Nginx 在 /home/download下下载压缩包 wget https://nginx.org/download/nginx-1.12.2.tar.gz 解压缩 tar zxvf ng ...
- Nagios+InfluxDB+Grafana
1. 概述 Nagios负责收集数据,是一款开源的免费网络监视工具. influxDB负责存储数据,是一个开源的时间序列数据库.比较适合存储监控或者部署记录这些时序数据. Grafana负责数据的图形 ...
- django LookUp
Custom Lookups 一个简单LookUp例子 Author.objects.filter(name__ne='Jack') # Translate SQL "author" ...
- 搜索实时个性化模型——基于FTRL和个性化推荐的搜索排序优化
本文来自网易云社区 作者:穆学锋 简介:传统的搜索个性化做法是定义个性化的标签,将用户和商品通过个性化标签关联起来,在搜索时进行匹配.传统做法的用户特征基本是离线计算获得,不够实时:个性化标签虽然具有 ...
- 【QTP专题】连接数据库
获取数据库连接串 在本地新建一个.txt文件,修改扩展名名*.udl:双击*.udl文件,打开数据库链接属性,定位到"提供程序"选显卡,选中如sqlserver的连接 Micro ...
- OI网络流 简单学习笔记
持续更新! 基本上只是整理了一下框架,具体的学习给出了个人认为比较好的博客的链接. ..怎么说呢,最基础的模板我就我不说了吧qwq,具体可以参考一下这位大佬写的博客:最大流,最小割,费用流 费用流 跑 ...