php 檔案圖片上傳類程式

來源:互聯網
上載者:User

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>php教程 檔案圖片上傳類程式</title>
</head>

<body>

<form enctype="multipart/form-data" action="upx.php" method="post">
 <input name="swfile" type="file">
 <input type="submit" value="上傳">
</form>
</body>
</html>
upx.php檔案
<?php
//上傳操作
require_once './libs/uploadx.php';
$upx = new uploadx();
$upx->uploadx_form = 'swfile';
$upx->uploadx_save = "upload";
$upx->uploadx_size = "1024";
$upx->uploadx_name = time();
$upx->file();
print_r($upx->file);

?>
uploadx.php類檔案
<?php
/*

使用方法:

html表單頁
-------------------------------------------------------------------------
<form enctype="multipart/form-data" action="upload.php" method="post">
 <input name="swfile" type="file">
 <input type="submit" value="上傳">
</form>
-------------------------------------------------------------------------


upload.php處理頁
-------------------------------------------------------------------------
<?php
require_once './uploadx.php';
$upx = new uploadx();
$upx->uploadx_form = 'swfile';              //表單控制項名稱(表單上傳控制項的名稱<input name="swfile" type="file" />)
$upx->uploadx_save = "temp";                //儲存檔案目錄(上傳檔案儲存目錄可以是相對路徑也可以是絕對路徑)
$upx->uploadx_type = 'jpg|gif|png|swf';    //允許上傳類型(根據尾碼限制上傳類型,每個尾碼用"|"隔開) 
$upx->uploadx_size = "1024";                //允許上傳大小(單位是kb。例:1024=1024kb)
$upx->uploadx_name = time();                //上傳後檔案名稱(可自訂。例:date("y-m-d",time()))

if($upx->file()){
  echo "上傳成功<br />";
  echo "名稱->".$upx->file['name']."<br />";
  echo "路徑->".$upx->file['path']."<br />";
  echo "大小->".$upx->file['size']."<br />";
  echo "類型->".$upx->file['type']."<br />";
  echo "時間->".$upx->file['time']."<br />";
  echo "結果->".$upx->file['info']."<br />";
 
}else{
  echo $upx->file['info'];
}

 

-------------------------------------------------------------------------
*/
class uploadx {

       
       public $uploadx_form; //表單控制項名稱
       public $uploadx_save; //儲存檔案目錄
       public $uploadx_type; //允許上傳類型
       public $uploadx_size; //允許上傳大小
       public $uploadx_name; //上傳後檔案名稱
      
       function __construct(){//初始化函數
          $this->uploadx_form = 'attach';
          $this->uploadx_save = 'temp';
          $this->uploadx_type = 'jpg|gif|png|swf|flv|rar|7z|zip|doc|docx|ppt|pptx|xls|xlsx|txt|pdf|wav|mp3|wma|rm|rmvb|wmv';        
          $this->uploadx_size = '1024';
          $this->uploadx_info = false;
       }
      
      function mkdirs($path , $mode = 0777){
          $rootdir = '';
          if(substr($path,0,1)=='/') $rootdir = $_server['document_root'];         
          $path = $rootdir . $path;
          if(!is_dir($path)){
              $this->mkdirs(dirname($path),$mode);
              mkdir($path,$mode);
          }
          return true;
      }

 

     function file(){
              if(!isset($_files[$this->uploadx_form])){
                  $this->file = array('file'=>false,'info' => '上傳錯誤!請檢查表單上傳控制項名稱['.$this->uploadx_form.']是否正確!');
                  return false;
              }
             
              switch($_files[$this->uploadx_form]['error']){
                
                 case 1:
                  $this->file = array('file'=>false,'info' => '指定上傳的檔案大小超出伺服器限制!');
                  return false;
                 break; 
                
                 case 2:
                  $this->file = array('file'=>false,'info' => '指定上傳的檔案大小超出表單限制!');
                  return false;
                 break; 
                                            
                 case 3:
                  $this->file = array('file'=>false,'info' => '只有部份檔案被上傳,檔案不完整!');
                  return false;
                 break; 
                                                     
                 case 4:
                  $this->file = array('file'=>false,'info' => '您沒有選擇上傳任何檔案!');
                  return false;
              }
             
              $postfix = pathinfo($_files[$this->uploadx_form]['name'], pathinfo_extension); 
              if(stripos($this->uploadx_type,$postfix)===false){
                $this->file = array('file'=>false,'info' => '指定上傳的檔案類型超出限制,允許上傳檔案類型:'.$this->uploadx_type);
                return false;
               
              }
         
              if(round($_files[$this->uploadx_form]['size']/1024)>$this->uploadx_size){
                $this->file = array('file'=>false,'info' => '指定上傳的檔案超出大小限制,檔案上傳限制範圍:'.$this->uploadx_size.'kb');
                return false; 
              }               
           
              if($this->mkdirs($this->uploadx_save)){             
                $this->uploadx_name = isset($this->uploadx_name) ? $this->uploadx_name.'.'.$postfix : $_files[$this->uploadx_form]['name'];
                if(!@move_uploaded_file($_files[$this->uploadx_form]['tmp_name'],$this->uploadx_save.'/'.$this->uploadx_name)){
                  $this->file = array('file'=>false,'info' => '上傳檔案儲存過程中出現錯誤,請檢查路徑或目錄許可權.');
                  return false;
                }
              }else{
                 $this->file = array('file'=>false,'info' => '伺服器目錄不存在,自動建立目錄失敗,請檢查是否有許可權!');
                  return false;
              }                
             
              @chmod($this->uploadx_save.'/'.$this->uploadx_name,0777);    
              $this->file = array(
                    'file' => true,
                    'name' => $this->uploadx_name,
                    'path' => $this->uploadx_save.'/'.$this->uploadx_name,
                    'size' => $_files[$this->uploadx_form]['size'],
                    'type' => $postfix,
                    'time' => time(),
                    'info' => '上傳成功!'
              );               
              return true;
                        
     }


}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.