http://www.cnblogs.com/itcx/p/4209034.html

upload.php

class File_upload{

public $upload_path='./upload/';//上传文件的路径

public $allow_type=array();//允许上传的文件类型

public $max_size='20480';//允许的最大文件大小

public $overwrite=false;//是否设置成覆盖模式

public $renamed=false;//是否直接使用上传文件的名称,还是系统自动命名

/**

* 私有变量

*/

private $upload_file=array();//保存上传成功文件的信息

private $upload_file_num=0;//上传成功文件的数目

private $succ_upload_file=array();//成功保存的文件信息

/**

* 构造器

*

* @param string $upload_path

* @param string $allow_type

* @param string $max_size

*/

public function __construct($upload_path='./upload/',$allow_type='jpg|bmp|png|gif|jpeg',$max_size='204800')

{

$this->set_upload_path($upload_path);

$this->set_allow_type($allow_type);

$this->max_size=$max_size;

$this->get_upload_files();

}

/**

* 设置上传路径,并判定

*

* @param string $path

*/

public function set_upload_path($path)

{

if(file_exists($path)){

if(is_writeable($path)){

$this->upload_path=$path;

}else{

if(@chmod($path,'0666'))

$this->upload_path=$path;

}

}else{

if(@mkdir($path,'0666')){

$this->upload_path=$path;

}

}

}

//设置上传文件类型

public function set_allow_type($types){

$this->allow_type=explode("|",$types);

}

//上传文件

public function get_upload_files()

{

foreach ($_FILES AS $key=>$field)

{

$this->get_upload_files_detial($key);

}

}

//上传文件数据存放到数组中

public function get_upload_files_detial($field){

if(is_array($_FILES["$field"]['name']))

{

for($i=0;$i

{

if(0==$_FILES[$field]['error'][$i])

{

$this->upload_file[$this->upload_file_num]['name']=$_FILES[$field]['name'][$i];

$this->upload_file[$this->upload_file_num]['type']=$_FILES[$field]['type'][$i];

$this->upload_file[$this->upload_file_num]['size']=$_FILES[$field]['size'][$i];

$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES[$field]['tmp_name'][$i];

$this->upload_file[$this->upload_file_num]['error']=$_FILES[$field]['error'][$i];

$this->upload_file_num++;

}

}

}

else {

if(0==$_FILES["$field"]['error'])

{

$this->upload_file[$this->upload_file_num]['name']=$_FILES["$field"]['name'];

$this->upload_file[$this->upload_file_num]['type']=$_FILES["$field"]['type'];

$this->upload_file[$this->upload_file_num]['size']=$_FILES["$field"]['size'];

$this->upload_file[$this->upload_file_num]['tmp_name']=$_FILES["$field"]['tmp_name'];

$this->upload_file[$this->upload_file_num]['error']=$_FILES["$field"]['error'];

$this->upload_file_num++;

}

}

}

/**

* 检查上传文件是构满足指定条件

*

*/

public function check($i)

{

if(!empty($this->upload_file[$i]['name'])){

//检查文件大小

if($this->upload_file[$i]['size']>$this->max_size*1024)$this->upload_file[$i]['error']=2;

//设置默认服务端文件名

$this->upload_file[$i]['filename']=$this->upload_path.$this->upload_file[$i]['name'];

//获取文件路径信息

$file_info=pathinfo($this->upload_file[$i]['name']);

//获取文件扩展名

$file_ext=$file_info['extension'];

//检查文件类型

if(!in_array($file_ext,$this->allow_type))$this->upload_file[$i]['error']=5;

//需要重命名的

if($this->renamed){

list($usec, $sec) = explode(" ",microtime());

$this->upload_file[$i]['filename']=$sec.substr($usec,2).'.'.$file_ext;

unset($usec);

unset($sec);

}

//检查文件是否存在

if(file_exists($this->upload_file[$i]['filename'])){

if($this->overwrite){

@unlink($this->upload_file[$i]['filename']);

}else{

$j=0;

do{

$j++;

$temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$this->upload_file[$i]['filename']);

}while (file_exists($temp_file));

$this->upload_file[$i]['filename']=$temp_file;

unset($temp_file);

unset($j);

}

}

//检查完毕

} else $this->upload_file[$i]['error']=6;

}

/**

* 上传文件

*

* @return true

*/

public function upload()

{

$upload_msg='';

for($i=0;$iupload_file_num;$i++)

{

if(!empty($this->upload_file[$i]['name']))

{

//检查文件

$this->check($i);

if (0==$this->upload_file[$i]['error'])

{

//上传文件

if(!@move_uploaded_file($this->upload_file[$i]['tmp_name'],$this->upload_file[$i]['filename']))

{

$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!
';

}else

{

$this->succ_upload_file[]=$this->upload_file[$i]['filename'];

$upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 成功了
';

}

}else $upload_msg.='上传文件'.$this->upload_file[$i]['name'].' 出错:'.$this->error($this->upload_file[$i]['error']).'!
';

}

}

echo $upload_msg;

}

//错误信息

public function error($error)

{

switch ($error) {

case 1:

return '文件大小超过php.ini 中 upload_max_filesize 选项限制的值';

break;

case 2:

return '文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';

break;

case 3:

return '文件只有部分被上传';

break;

case 4:

return '没有文件被上传';

break;

case 5:

return '这个文件不允许被上传';

break;

case 6:

return '文件名为空';

break;

default:

return '出错';

break;

}

}

//获取成功的数据信息为数组(备用)

public function get_succ_file(){

return $this->succ_upload_file;

}

}

$upload=new File_upload('./upload/','jpg|bmp|png|gif|jpeg');

$upload->upload();

$t=$upload->get_succ_file();

print_r($t);

转载于:https://www.cnblogs.com/itcx/p/4209034.html

标签:文件,name,upload,field,file,error,path,php,上传

来源: https://blog.csdn.net/weixin_30338497/article/details/95934199

php文件多上传文件,php文件上传(多文件上传)相关推荐

  1. [转]文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总1

    转自:http://blog.csdn.net/soarheaven/archive/2008/12/08/3474152.aspx 最近项目需要对FTP服务器进行操作,现把实现总结如下: 打算分2篇 ...

  2. java 文件上传 jar_JavaWeb 之 使用 commons-fileupload.jar 实现文件上传

    一.文件上传 文件的上传和下载,是非常常见的功能,在很多的系统中,或软件中都经常使用文件的上传和下载 文件的上传主要分为下面几步: 1.前台页面需要一个 form 标签,里面的 method 为 po ...

  3. Winform中实现FTP客户端并定时扫描指定路径下文件上传到FTP服务端然后删除文件

    场景 Windows10上怎样开启FTP服务: Windows10上怎样开启FTP服务_BADAO_LIUMANG_QIZHI的博客-CSDN博客 上面在Windows上搭建FTP服务器之后,会接收客 ...

  4. c#如何通过ftp上传文件_ftp自动上传工具,ftp自动上传工具如何自动上传文件

    不知道大家用过ftp自动上传文件的ftp上传工具吗?小编到现在为止也只用过一款ftp上传工具是具有定时功能的.定时这个功能是真的很棒了,节省了很多时间而且还很方便快捷.ftp自动上传文件怎么上传?下面 ...

  5. android 上传html文件大小,浅谈关于Android WebView上传文件的解决方案

    我们在开发需求的时候,难免会接入一下第三方的H5页面,有些H5页面是具有上传照片的功能,Android 中的 WebView是不能直接打开文件选择弹框的 接下来我讲简单提供一下解决方案,先说一下思路 ...

  6. aws php 上传文件 限制大小_如何压缩PDF文件大小,满足各种上传大小要求

    介绍 今天我们来说一个小技巧,就是对PDF文件大小的压缩.那么这个问题是怎么来的呢,我们在系统上传PDF文件的时候,由于系统限制,PDF大小受到了限制,我们需要对PDF进行压缩小一点进行上传,才能满足 ...

  7. ewebeditor在上传文件时,总是提示“请选择一个有效的文件”,

    用ewebeditor在上传文件时,总是提示"请选择一个有效的文件",可我上传的文件格式明明是正确的,而且在XP上测试时一切正常啊,难道是程序有问题?经过研究终于找到了问题所在. ...

  8. vue 文件及描述信息一起上传_用Vue实现一个大文件上传和断点续传

    前言 这段时间面试官都挺忙的,频频出现在博客文章标题,虽然我不是特别想蹭热度,但是实在想不到好的标题了-.-,蹭蹭就蹭蹭 :) 事实上我在面试的时候确实被问到了这个问题,而且是一道在线 coding ...

  9. Linux下使用shell实现上传linux下某个目录下所有文件到ftp

    首先我们需要搞清楚单个文件怎么上传,把这个单文件上传到ftp上的实现命名为一个:upload_to_ftp_command.sh 之后,需要弄清楚怎么实现遍历一个目录下的所有文件的,把这个遍历某个目录 ...

  10. uploadify java 上传_jquery使用uploadify插件实现多文件的上传(java版)

    2.安装,由于下载下来的例子是php版本的,所以我只留下了主要的几个文件.如图: 4.使用 前台页面: pageEncoding="UTF-8"%> html PUBLIC  ...

最新文章

  1. 前嗅ForeSpider教程:网站登录配置
  2. eclipse常用设置之自动格式化
  3. python程序如何循环_在Python的一段程序中如何使用多次事件循环详解
  4. 2D转换之缩放scale(CSS3)
  5. 卷积的感受野计算及特征图尺寸计算
  6. 致敬Github那些卓越贡献的大佬和他们的公众号
  7. android wifi直连共享文件,让Android支持AD-hoc方式连WIFI(笔记本直接用WIFI共享给手机)的方法...
  8. java编写监听器步骤_IT兄弟连 JavaWeb教程 监听器1
  9. 第四天 轨道交通仿真入门
  10. 怎么关闭火狐浏览器的百度辅助模式(无障碍服务)
  11. 资源-Windows10-2020原版镜像下载地址(20H2)以及1809、1803、1709
  12. 行高line-height,以及基线、顶线、中线和底线,还有内容区域、行内框和行框 by 豆豆猫的窝...
  13. 前端从一只小白到工作半年的心路历程
  14. 单核工作法图解_摆脱穷忙,加强自制力:《单核工作法图解》助你居家办公更专一...
  15. 私有云厂商云宏破解金融行业转型“数字底座”难题
  16. 操作系统面试问题集锦
  17. 学习笔记之VOIP网守的功能
  18. 转:从头开始编写基于隐含马尔可夫模型HMM的中文分词器
  19. FT2004(D2000)开发实战之网口stmmac报错调试(Failed to reset the dma)
  20. 量化交易之回测篇 - 拉取合成历史沉淀资金数据(主连合约)

热门文章

  1. 512M内存编译php出错
  2. CSS只是进化的一部分
  3. ASP.NET 数据库缓存依赖
  4. 进入保护模式(三)内存的分页
  5. 在webservice中传递Hashtable
  6. 北方工业大学gpa计算_北方大学联盟仓库的探索性分析
  7. MySQL-InnoDB索引实现
  8. leetcode 338. 比特位计数
  9. leetcode457. 环形数组循环
  10. istio 和 kong_如何启动和运行Istio