php多文件传输 多图片上传程序代码
多文件上传其实就包括了图片及各种文件了,下面介绍的是一款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/>'; }//开源代码Cuoxin.com } } function getSaveInfo() { return $this->save_info; } } //$upload = new UploadFile('',''); //$upload = new UploadFile(); //$upload->showErrorInfo(); ?> (编辑:银川站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |