php多文件上传 多图片上传程序代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-22 16:36:53 浏览: 评论:0
多文件上传其实就包括了图片及各种文件了,下面介绍的是一款PHP多文件上传类,一共两个文件,upp.php 和 uploadFile.php,upp.php,这是前台所显示的表单文件了,默认的是四个上传文件域,我们可以手动进行修改,另外这个页面嵌套了 uploadFile.php 文件上传类,下面一起来看例子.
php文件上传例子,代码如下:
- <?php
- header('content-type:text/html;charset=utf-8');
- require('uploadFile.php');
- if(isset($_POST['submit'])){
- $uploads = $_FILES['file'];
- $num_file = count($uploads['name']);
- $up = new UploadFile($uploads,'uploads',1024);
- $num = $up->upload();
- if($num == $num_file ){
- echo '全部文件上传成功';
- exit;
- }else{
- echo $num,'个文件上传成功<br/>';
- echo $up->showErrorInfo();
- exit;
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.111cn.net/ 1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <form action="uup.php" method="post" enctype="multipart/form-data">
- <p><input name="file[]" type="file" /></p>
- <p><input name="file[]" type="file" /></p>
- <p><input name="file[]" type="file" /></p>
- <p><input name="file[]" type="file" /></p>
- <input name="submit" type="submit" />
- </form>
- </body>
- </html>
PHP文件上传类代码,代码如下:
- <?php
- /*------------*/
- class UploadFile
- {
- var $user_post_file = array();
- var $save_file_path = '';
- var $max_file_size = '';
- var $allow_type = array('gif','jpg','png','zip','rar','txt','doc','pdf');
- var $final_file_path = '';
- var $save_info = array();
- var $error_info = array();
- /**
- *构造函数,用于初始化信息。
- *
- *@param Array $file
- *@param String $path
- *@param Integer $size
- *@param Array $type
- */
- function __construct($file,$path,$size = 2097152,$type='')
- {
- $this->user_post_file = $file;
- $this->save_file_path = $path;
- $this->max_file_size = $size;
- if(!$type=''){
- $this->allow_type[] = $type;
- }
- }
- /**
- *
- *
- *@access public
- *@return int
- */
- function upload()
- {
- for($i=0;$i<count($this->user_post_file['name']);$i++)
- {
- if($this->user_post_file['error'][$i] == 0){//上传文件状态正常
- //获取当前文件名,临时文件名,大小,类型,扩展名
- $name = $this->user_post_file['name'][$i];
- $tmp_name = $this->user_post_file['tmp_name'][$i];
- $size = $this->user_post_file['size'][$i];
- $type = $this->user_post_file['type'][$i];
- $ext_name = $this->getExtName($name);
- //文件大小
- if(!$this->checkSize($size)){
- $this->error_info[] = '您上传的文件:'.$name.'太大';
- continue;
- }
- //扩展名
- if(!$this->checkType($ext_name)){
- $this->error_info[] = '您上传的文件:'.$name.'不合法';
- continue;
- }
- //非法上传
- if(!is_uploaded_file($tmp_name)){
- $this->error_info[] = '您上传的文件:'.$name.'属于非法提交';
- continue;
- }
- //
- $basename = $this->getBaseName($name,".".$ext_name);
- $final_filename = $basename.'-'.time().'-'.rand(1,10000).'.'.$ext_name;
- $this->final_file_path = $this->save_file_path.'/'.$final_filename;
- if(!move_uploaded_file($tmp_name,$this->final_file_path)){
- $this->error_info = $this->user_post_file['error'][$i];
- continue;
- }
- //
- $this->save_info[] = array(
- "name" => $name,
- "ext_name" => $ext_name,
- "type" => $type,
- "size" => $size,
- "final_filename" => $final_filename,
- "path" => $this->final_file_path
- );
- }
- }
- return count($this->save_info);
- }
- /*
- *检查用户上传文件的大小时候合法
- *
- *@param Integer $size
- *@access private
- *@return boolean
- */
- function checkSize($size)
- {
- if($size > $this->max_file_size){
- return FALSE;
- }
- return TRUE;
- }
- /*
- *检查用户上传文件的类型是否合法
- *
- *@access private
- *@return boolean
- */
- function checkType($extension)
- {
- foreach($this->allow_type as $type){
- if(strcasecmp($extension,$type) == 0){
- return TRUE;
- }
- }
- return FALSE;
- }
- /*
- *获取文件的扩展名
- *
- *@param string $filename
- *@access private
- *@return string
- */
- function getExtName($filename)
- {
- $p = pathinfo($filename);
- return $p['extension'];
- }
- /*
- *获取文件名(不包括扩展名)
- *
- *@param string $filename
- *@param string $type
- *@access private
- *@return boolean
- */
- function getBaseName($filename,$ext_name)
- {
- $basename = basename($filename,$ext_name);
- return $basename;
- }
- /*
- *
- *
- *
- */
- function showErrorInfo()
- {
- if(count($this->error_info) != 0){
- //echo 'error...<br/>';
- foreach($this->error_info as $k=>$v){
- echo ($k+1),':',$v,'<br/>';
- }//开源代码phpfensi.com
- }
- }
- function getSaveInfo()
- {
- return $this->save_info;
- }
- }
- //$upload = new UploadFile('','');
- //$upload = new UploadFile();
- //$upload->showErrorInfo();
- ?>
Tags: php多文件上传 php多图片上传
- 上一篇:PHP图片上传实例分析
- 下一篇:PHP简单文件下载函数
相关文章
- ·php 实现多文件上传程序代码(2013-11-13)
- ·php多文件上传与上传文件原理分析(2014-08-04)
- ·php 实现多文件上传程序代码(2014-08-28)
- ·PHP多文件上传理解总结(2014-08-28)
- ·三款php多文件上传实例代码(2014-09-09)
- ·PHP实现多文件上传的方法(2021-06-09)
- ·PHP多文件上传实例(2021-06-11)
- ·PHP实现文件上传和多文件上传(2021-07-01)
- ·PHP单文件和多文件上传实例(2021-08-23)
- ·php实现多图片上传程序代码(2015-12-24)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)