PHP单文件上传的过程化函数封装
来源:互联网 发布:照片拼在一起无缝软件 编辑:程序博客网 时间:2024/05/29 07:58
提交文件的页面:
upload.php
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>文件上传</title></head><body> <form action="doAction3.php" method="post" enctype="multipart/form-data"> <!-- 在客户端可以设置表单MAX_FILE_SIZE限制的大小 --> <!--<input type="hidden" name="MAX_FILE_SIZE" value="5*1024*1024">--> 请选择您要上传的文件: <!-- 在客户端设置允许上传的文件类型 --><!-- 但是我们需要记住:客户端所做的任何限制都是不安全的 --> <!--<input type="file" name="myFile" accept="image/jpeg,image/gif,image/png"/><br/>--> <input type="file" name="myFile"/><br/> <input type="submit" value="上传文件"/> </form></body></html>
上传文件的函数 upload.func.php
<?php/** * Created by PhpStorm. * User: DreamBoy * Date: 2016/4/8 * Time: 20:13 *///$fileInfo = $_FILES['myFile'];function uploadFile($fileInfo,$uploadPath = 'uploads',$allowExt=array('jpeg','jpg','png','gif'),$maxSize = 2097152, $flag=true) { //判断错误号 if($fileInfo['error'] > 0) { //匹配错误信息 switch($fileInfo['error']) { case 1: $mes = '上传文件超过了PHP配置文件中upload_max_filesize选项的值'; break; case 2: $mes = '超过了表单MAX_FILE_SIZE限制的大小'; break; case 3: $mes = '文件部分被上传'; break; case 4: $mes = '没有选择上传文件'; break; case 6: $mes = '没有找到临时目录'; break; case 7: case 8: $mes = '系统错误'; break; } exit($mes); } $ext = pathinfo($fileInfo['name'], PATHINFO_EXTENSION); //$allowExt = array('jpeg', 'jpg', 'png', 'gif'); if(!is_array($allowExt)) { exit('系统错误'); } //检测上传文件的类型 if(!in_array($ext, $allowExt)) { exit('非法文件类型'); } //$maxSize = 2097152; //2M //检测上传文件大小是否符合规范 if($fileInfo['size']>$maxSize) { exit('上传文件过大'); } //检测图片是否为真实的图片类型 //$flag = true; if($flag) { if(!getimagesize($fileInfo['tmp_name'])) { exit('不是真实图片类型'); } } //检测文件是否是通过HTTP POST方式上传上来的 if(!is_uploaded_file($fileInfo['tmp_name'])) { exit('文件不是通过HTTP POST方式上传的'); } //$uploadPath = 'uploads'; if(!file_exists($uploadPath)) { mkdir($uploadPath, 0777, true); chmod($uploadPath, 0777); } $uniName = md5(uniqid(microtime(true), true)) . '.' . $ext; $destination = $uploadPath . '/' . $uniName; if(!@move_uploaded_file($fileInfo['tmp_name'], $destination)) { exit('文件移动失败'); } //echo '文件上传成功'; return array( 'newName' => $destination, 'size' => $fileInfo['size'], 'type' => $fileInfo['type'], );}
提交表单的处理文件 doAction3.php
<?php/** * Created by PhpStorm. * User: DreamBoy * Date: 2016/4/8 * Time: 20:29 */header('content-type:text/html;charset=utf-8');include_once 'upload.func.php';$fileInfo = $_FILES['myFile'];//$file = uploadFile($fileInfo);//$file = uploadFile($fileInfo, 'MyFiles');$allowExt = array('jpeg', 'jpg', 'png', 'gif', 'html', 'txt');$file = uploadFile($fileInfo, 'MyFiles', false, $allowExt);print_r($file);
0 0
- PHP单文件上传的过程化函数封装
- PHP实现单文件上传、多个单文件上传、多文件上传的过程化封装
- PHP单文件上传原理及上传函数的封装
- PHP单文件、多个单文件、多文件上传函数的封装
- PHP单文件上传(含封装函数)
- 简单文件上传函数的封装(PHP)
- php封装单文件上传到数据库(路径)
- PHP上传文件封装
- php 单文件上传
- php--单文件上传
- PHP单文件上传
- php单文件上传
- PHP的单文件上传类
- PHP的单文件上传类
- 文件上传与下载学习笔记(2)---多文件、单文件、多个单文件函数封装
- PHP文件上传类封装
- php基础之文件的上传过程
- PHP文件上传功能 -- 单文件上传
- opencv 模版匹配matchtemplate方法介绍
- css 复习-position定位
- <C#入门经典>学习笔记之结构、数组及字符串
- 狸猫的笔试——镜像字符串(二叉树法)
- iOS 登录
- PHP单文件上传的过程化函数封装
- POJ 1276 Cash Machine 多重背包
- hive数据类型及其数据转换
- 因祸得福——创建视图时改变字段的类型
- c++ 友元类
- 蓝桥杯 第七届省赛试题 方格填数
- 细说dex2oat(3)
- vi 编辑器
- craps赌博游戏