自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP上傳類upload.php的具體使用方法

開發(fā) 后端
PHP上傳類upload.php的主要作用就是幫助我們實(shí)現(xiàn)文件上傳的功能,希望讀者朋友在看后能夠?qū)嵺`操作一回,加深對(duì)這一類的理解。

我們今天為大家?guī)?lái)的是關(guān)于PHP上傳類upload.php 的具體用法,希望廣大讀者朋友能夠通過(guò)本文所介紹的內(nèi)容得到幫助,增加我們的知識(shí)點(diǎn)。

#t#具體代碼如下:

  1. <?php 
  2.  
  3. /**  
  4.  * 我的文件上傳類  
  5.  *  
  6.  * 未完成的功能:  
  7.  * 1.對(duì)目標(biāo)目錄是否存在的判斷  
  8.  * 2.如果上傳時(shí)出現(xiàn)重名,自動(dòng)重命名  
  9.  *   
  10.  * @author M.Q. <[url]www.mengqi.net[/url]> 
  11.  */  
  12. class upload  
  13. {  
  14.     /**  
  15.      * PHP上傳類upload.php上傳文件的信息,此值由構(gòu)造函數(shù)取得,如果上傳文件失敗或出錯(cuò)或未上傳,則此值為false  
  16.      *  
  17.      * @var array  
  18.      */  
  19.     private $file = false;   
  20.       
  21.      
  22.     /**  
  23.      * 構(gòu)造函數(shù):取得上傳文件的信息  
  24.      *   
  25.      * 如果在上傳文件的工程中發(fā)生錯(cuò)誤,那么出錯(cuò)的文件不會(huì)放在結(jié)果中返回,結(jié)果中的文件都是可用的  
  26.      *  
  27.      * @param string $tag form表單中<input>標(biāo)簽中name屬性的值,例<input name="p" type="file"> 
  28.      *   
  29.      * 例1,上傳單個(gè)文件:  
  30.      * <input name="upfile" type="file"> 
  31.      *   
  32.      * 例2,上傳多個(gè)文件:  
  33.      * <input name="upfile[]" type="file"> 
  34.      * <input name="upfile[]" type="file"> 
  35.      *   
  36.      * 結(jié)果(保存在$file變量中)如下:  
  37.      *   
  38.      * array(  
  39.      * [0] => array(  
  40.      *      'name'      => 'abc.txt'  
  41.      *      'type'      => 'text/plain’  
  42.      *      'tmp_name'  => '/tmp/phpgxecCb'  
  43.      *      'error'     => 0  
  44.      *      'size'      => 62  
  45.      *      )  
  46.      * [1] => array(  
  47.      *      'name'      => 'abc.txt'  
  48.      *      'type'      => 'text/plain’  
  49.      *      'tmp_name'  => '/tmp/phpgxecCb'  
  50.      *      'error'     => 0  
  51.      *      'size'      => 62  
  52.      *      )  
  53.      * )  
  54.      */  
  55.     public function __construct($tag)  
  56.     {  
  57.         $file = $_FILES[$tag];  
  58.                  
  59.         if (!isset($file) || empty($file))   
  60.         {  
  61.             return; //沒(méi)有上傳文件  
  62.         }  
  63.           
  64.         $num = count($file['name']); //PHP上傳類upload.php上傳文件的個(gè)數(shù)  
  65.           
  66.         $data = array(); //用來(lái)保存上傳文件的信息的數(shù)組  
  67.  
  68.         //上傳了多個(gè)文件  
  69.         if ($num > 1)  
  70.         {  
  71.             for($i = 0; $i < $num; $i++)  
  72.             {  
  73.                 $d = array();  
  74.                 $d['name']       = $file['name'][$i];  
  75.                 $d['type']       = $file['type'][$i];  
  76.                 $d['tmp_name']   = $file['tmp_name'][$i];  
  77.                 $d['error']      = $file['error'][$i];  
  78.                 $d['size']       = $file['size'][$i];  
  79.                   
  80.                 if ($d['error'] == 0)  
  81.                 {  
  82.                     $data[] = $d;  
  83.                 }  
  84.                 else   
  85.                 {  
  86.                     @unlink($d['tmp_name']);  
  87.                 }  
  88.             }  
  89.         }  
  90.         //只上傳了一個(gè)文件  
  91.         else   
  92.         {  
  93.             $d = array();  
  94.             $d['name']       = $file['name'];  
  95.             $d['type']       = $file['type'];  
  96.             $d['tmp_name']   = $file['tmp_name'];  
  97.             $d['error']      = $file['error'];  
  98.             $d['size']       = $file['size'];  
  99.               
  100.             if ($d['error'] == 0)  
  101.             {  
  102.                 $data[] = $d;  
  103.             }  
  104.             else   
  105.             {  
  106.                 @unlink($d['tmp_name']);  
  107.             }  
  108.         }  
  109.           
  110.         if (empty($data)) return;  
  111.           
  112.         $this -> file = $data; //保存上傳文件的信息  
  113.     }  
  114.       
  115.     /**  
  116.      * 將上傳的文件從臨時(shí)文件夾移動(dòng)到目標(biāo)路徑  
  117.      *  
  118.      * @param array $src 文件信息數(shù)組,是$file數(shù)組的其中一個(gè)元素(仍然是數(shù)組)  
  119.      * @param string $destpath 上傳的目標(biāo)路徑  
  120.      * @param string $filename 上傳后的文件名,如果為空,則使用上傳時(shí)的文件名  
  121.      * @return bool  
  122.      */  
  123.     public function save($src, $destpath, $filename = null)  
  124.     {  
  125.         $srcTName = $src['tmp_name']; //原始上傳文件的臨時(shí)文件名  
  126.         $srcFName = $src['name'];     //原始文件名  
  127.           
  128.         //如果$filename參數(shù)為空,則使用上傳時(shí)的文件名  
  129.         if (empty($filename))  
  130.         {  
  131.             $filename = $srcFName;  
  132.         }  
  133.           
  134.         //$dest是文件最終要復(fù)制到的路徑和文件名  
  135.         if (empty($destpath))  
  136.         {  
  137.             $dest = $filename;  
  138.         }  
  139.         else   
  140.         {  
  141.             //修正路徑中的斜杠,將末尾的\修改為/,如果末尾不是\也不是/,則給末尾添加一個(gè)/  
  142.             $pathend = $destpath[strlen($destpath) - 1]; //上傳的目標(biāo)路徑的***一個(gè)字符  
  143.             if ($pathend == '\\')  
  144.             {  
  145.                 $dest = substr_replace($destpath, '/', strlen($destpath)-1).$filename;  
  146.             }  
  147.             else if ($pathend != '/')  
  148.             {  
  149.                 $dest = $destpath.'/'.$filename;  
  150.             }  
  151.             else   
  152.             {  
  153.                 $dest = $destpath.$filename;  
  154.             }  
  155.         }  
  156.             
  157.         //上傳文件成功  
  158.         if (@move_uploaded_file($srcTName, $dest))  
  159.         {  
  160.               
  161.             return true;   
  162.         }  
  163.         else   
  164.         {  
  165.             return false;  
  166.         }  
  167.     }  
  168.       
  169.     /**  
  170.      * 取得上傳文件的信息  
  171.      *  
  172.      * @return array  
  173.      */  
  174.     public function getFileInfo()  
  175.     {  
  176.         return $this->file;  
  177.     }  
  178. }  
  179.  
  180.  
  181. $a = new upload('upfile');  
  182.  
  183. $fileinfo = $a -> getFileInfo();  
  184. if ($fileinfo == false)  
  185. {  
  186.     echo '沒(méi)有上傳文件!';  
  187.     exit;  
  188. }  
  189.  
  190. for($i = 0; $i < count($fileinfo); $i++)  
  191. {  
  192.     echo '正在上傳 '.$fileinfo[$i]['name'].' ';  
  193.     if ($a -> save($fileinfo[$i], 'upload')) echo '完畢';  
  194.     else echo '失敗';  
  195.     echo '<br>';  
  196. }  
  197. ?> 

以上代碼就是關(guān)于PHP上傳類upload.php的具體使用方法。

責(zé)任編輯:曹凱 來(lái)源: CSDN
相關(guān)推薦

2009-12-02 18:51:12

PHP分頁(yè)類

2009-12-07 16:52:59

PHP函數(shù)getima

2009-11-24 19:25:32

PHP關(guān)聯(lián)數(shù)組

2009-11-26 19:05:04

PHP函數(shù)explod

2009-11-26 15:23:24

PHP函數(shù)ereg()

2009-11-25 10:02:27

PHP會(huì)話Sessio

2009-12-01 17:00:49

PHP變量

2009-12-01 19:02:20

PHP取整函數(shù)

2009-12-01 18:02:41

PHP表單數(shù)組

2009-12-02 14:50:25

PHP接口類inter

2009-11-16 13:57:21

PHP上傳文件

2009-11-30 18:08:30

PHP制作動(dòng)態(tài)計(jì)數(shù)器

2009-11-24 15:01:59

PHP通用文件上傳類

2009-12-02 16:04:44

PHP fsockop

2009-11-30 15:00:19

PHP加密解密函數(shù)au

2009-11-24 16:18:14

PHP5析構(gòu)函數(shù)

2011-07-12 17:11:13

PHPSHELL

2009-11-30 17:43:54

PHP split()

2011-06-16 11:01:56

PHP繼承

2009-12-02 15:02:09

PHP simplex
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)