程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 文件圖片上傳類程序

php 文件圖片上傳類程序

編輯:關於PHP編程

<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;
                        
     }


}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved