php檔案上傳代碼(支援檔案批量上傳)

來源:互聯網
上載者:User

本款檔案上傳類,預設是上傳單檔案的,我們只要修改$inputname ='files'為你的表單名就可以方便的實現批量檔案上傳了。 $savename = ''儲存檔案名稱, $alowexts = array()設定允許上傳的類型,$savepath = ''儲存路徑。
*/

 代碼如下 複製代碼

class upload
{
 public $savepath;
 public $files;
 private $error;

 function __construct($inputname ='files', $savepath = '', $savename = '', $alowexts = array(),$maxsize = 1024000)
 {
  if(!$alowexts)$alowexts=explode('|',upload_ftype);
  $file_array=array();
  $savepath=str_replace('','/',$savepath);
  $savename=preg_replace('/[^a-z0-9_]+/i','',$savename);
  $this->savepath=substr($savepath,-1)=='/'?$savepath:$savepath.'/'; //路徑名以/結尾

  if(!make_dir($this->savepath))
  {
   $this->error=8;
   $this->error();
  }
  //exit($this->savepath);
  if(!is_writeable($this->savepath))
  {
   $this->error=9;
   $this->error();
  }
  if(sizeof($_files[$inputname]['error'])>10)
  {
   $this->error=13;
   $this->error();
  }
  $max=sizeof($_files[$inputname]['error'])-1;
  //exit($this->savepath.$savename);
  foreach($_files[$inputname]['error'] as $key => $error)
  {
   if($error==upload_err_ok) //批量上傳
   {
    $savename=$savename?$savename:date('ymdims').mt_rand(10000,99999);
    $fileext=strtolower(get_fileext($_files[$inputname]['name'][$key]));
    $savename=$savename.'.'.$fileext;
    $tmp_name=$_files[$inputname]['tmp_name'][$key];
    $filesize=$_files[$inputname]['size'][$key];
    if(!in_array($fileext,$alowexts))
    {
     $this->error=10;
     $this->error();
    }
    if($filesize>$maxsize)
    {
     $this->error=11;
     $this->error();
    }
    if(!$this->isuploadedfile($tmp_name))
    {
     $this->error=12;
     $this->error();
    }

    if(move_uploaded_file($tmp_name,$this->savepath.$savename) || @copy($tmp_name,$this->savepath.$savename))
    {
     //exit($this->savepath.$savename);
     @chmod($savename, 0644);
     @unlink($tmp_name);
     $file_array[]=$this->savepath.$savename;     
    }
   }
   else
   {
    $this->error=$error;
    $this->error();
   }
   unset($savename);
  }
  $this->files=$file_array;
  return true;
 }

 function isuploadedfile($file) //去掉系統內建的反斜線
 {
  return (is_uploaded_file($file) || is_uploaded_file(str_replace('\','',$file)));
 }

 function error()
 {
  $upload_error=array(0 => '檔案上傳成功 !',
       1 => '上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值 !',
       2 => '上傳檔案的大小超過了 html 表單中 max_file_size 選項指定的值 !',
       3 => '檔案只有部分被上傳 !',
       4 => '沒有檔案被上傳 !',
       5 => '未知錯誤!',
       6 => '找不到臨時檔案夾。 !',
       7 => '檔案寫入臨時檔案夾失敗 !',
       8 => '附件目錄建立失敗 !',
       9 => '附件目錄沒有寫入許可權 !',
       10 => '不允許上傳該類型檔案 !',
       11 => '檔案超過了管理員限定的大小 !',
       12 => '非法上傳檔案 !',
       13 => '最多可同時上傳10個檔案 !'
       );
  showmsg($upload_error[$this->error]);
 }

}

//使用方法

new upload();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.