這個文件上傳類可以實現多個文件或單個文件進行上傳了,下面小編來給各位推薦一個不錯的例子,實例代碼如下:
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.49028c.com/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>無標題文檔</title>
- </head>
- <body>
- <?php
- //php文件上傳類(該類支持單個或者多個文件上傳)
- /**
- * 類名:upfile
- * 作用:處理文件上傳
- * 說明,該類處理單個或者多個文件上傳,使用該類時,只需要實列化該類
- * 例:
- * $up = upfile()
- * $up->update_file($_file['filename'])
- *
- * $up->update_file 函數返回一個數組,如果是多文件上傳,則為多維數據。
- * 數組的內容:
- * $fileinfo['file_size'] 上傳文件的大小
- * $fileinfo['file_suffix'] 上傳文件的類型
- * $fileinfo['file_name'] 上傳文件的名字
- * $fileinfo['error'] 上傳文件產生的錯誤
- *
- */
- class upfile {
- public $fcount = 1; //上傳文件的數量
- public $ftype = array('jpg','jpeg','gif','png'); //文件格式
- public $fsize = 1024; //文件大小單位kb
- public $fdir = 'www.111cn.net/'; //文件存放目錄
- public $errormsg = ''; //產生的臨時錯誤信息
- /**
- *函數名:get_tmp_file($putfile)
- *作用:取得上傳的臨時文件名
- *@param array $putfile
- *@return string $upimg 返回臨時文件名
- */
- function get_tmp_file($putfile){
- if($this->fcount == 1){
- $tmpfile = $putfile['tmp_name'];
- }else{
- for($i=0;$i<$this->fcount;$i++){
- $tmpfile[] = $putfile['tmp_name'][$i];
- }
- }
- return $tmpfile;
- }
- /**
- *函數名:get_suffix($filename)
- *作用:取得文件的后綴名
- *@param file $filename
- *@return string $suffixname 返回后綴名
- */
- function get_suffix($filename){
- $link = pathinfo($filename);
- $suffixname = strtolower($link['extension']);
- return $suffixname;
- }
- /**
- *驗證文件大小
- *@author 趙紅健
- *@param $filesize
- *@return booln
- */
- function check_file_size($filesize){
- $this->errormsg = '';
- if($filesize/1000 > $this->fsize){
- $this->errormsg = '警告:文件超出大小!';
- return false;
- }else{
- return true;
- }
- }
- /**
- *驗證文件類型是否合法
- *@author 趙紅健
- *@param $filesuffix
- *@return booln
- */
- function check_file_suffix($filesuffix){
- $this->errormsg = '';
- if(!in_array($filesuffix,$this->ftype)){
- $this->errormsg = '警告:文件類型不在允許范圍內!';
- return false;
- }else{
- return true;
- }
- }
- /**
- *移動臨時文件
- *@author 趙紅健
- *@param $filesuffix
- *@return booln
- */
- function move_temp_file($tmpfile,$targetfile){
- $this->errormsg = '';
- if(!move_uploaded_file($tmpfile,$targetfile)){
- $this->errormsg = '警告:文件移動失??!';
- return false;
- }else{
- return true;
- }
- }
- /**
- *函數名:update_file($putfile)
- *作用:上傳文件
- *@param array $putfile
- *@return array 文件信息
- */
- function update_file($putfile){
- $tmpfile = $this->get_tmp_file($putfile);
- if(!file_exists($this->fdir)){
- $this->errormsg[] = '錯誤:目錄'.$this->fdir.'不存在';
- return $this->errormsg;
- }
- $this->fdir = substr($this->fdir,strlen($this->fdir)-1,1)=='/'?$this->fdir:$this->fdir.'/';
- if(!is_array($putfile['size'])){
- $fileinfo['file_size'] = $putfile['size'];
- if(!$this->check_file_size($fileinfo['file_size'])){
- $fileinfo['error'] = $this->errormsg;
- return $fileinfo;
- }
- $fileinfo['file_suffix'] = $this->get_suffix($putfile['name']);
- if(!$this->check_file_suffix($fileinfo['file_suffix'])){
- $fileinfo['error'] = $this->errormsg;
- return $fileinfo;
- }
- $fileinfo['file_name'] = date('ymdhms').'.'.$fileinfo['file_suffix'];
- if(!$this->move_temp_file($tmpfile,$this->fdir.$fileinfo['file_name'])){
- $fileinfo['error'] = $this->errormsg;
- return $fileinfo;
- }
- return $fileinfo;
- }else{
- for($i=0;$i<$this->fcount;$i++){
- $fileinfo[$i]['file_size'] = $putfile['size'][$i];
- if(!$this->check_file_size($fileinfo[$i]['file_size'])){
- $fileinfo[$i]['error'] = $this->errormsg;
- continue;
- }
- $fileinfo[$i]['file_suffix'] = $this->get_suffix($putfile['name'][$i]);
- if(!$this->check_file_suffix($fileinfo[$i]['file_suffix'])){
- $fileinfo[$i]['error'] = $this->errormsg;
- continue;
- }
- $fileinfo[$i]['file_name'] = date('ymdhms').rand().'.'.$fileinfo[$i]['file_suffix'];
- if(!$this->move_temp_file($tmpfile[$i],$this->fdir.$fileinfo[$i]['file_name'])){
- $fileinfo[$i]['error'] = $this->errormsg;
- continue;//開源代碼Vevb.com
- }
- }
- return $fileinfo;
- }
- }
- }
- ?>
- </body>
- </html>
新聞熱點
疑難解答