php 經典安全檔案上傳代碼

來源:互聯網
上載者:User

<?php
/**
 * 上傳檔案
 * to : uploadBoroughThumb|borough|picture
 * to : 函數名|目錄分類|圖片類型
 *
 */
require('path.inc.php');
$to = $_GET["to"];
$action = $_GET['action'];
if($action==""){
 $action = "form";
}
if($action=="doupload"){
 echo '<html>';
 echo '<head>';
 echo '<title>上傳成功</title>';
 echo "<meta http-equiv="content-type" content="text/html; charset=gb2312">";
 echo '</head>';
 
 $store_info = explode('|',$to);
 $js_func = $store_info[0];

 /*  判斷特殊字元 */
 if($store_info[1]){
  if(!ereg("^[A-Za-z]+$",$store_info[1])){
   exit;
  }
 }
 if($store_info[2]){
  if(!ereg("^[A-Za-z]+$",$store_info[2])){
   exit;
  }
 }

 $upload_conf = require($cfg['path']['conf'].'upload.cfg.php');
 
 $this_config = (array)$upload_conf[$store_info[1]][$store_info[2]];
 if(empty($this_config)){
  exit;
 }
 $upload = new UploadFile();//執行個體化上傳對象
 //設定可以上傳檔案的類型
 $upload->setAllowFileType($this_config['allowType']);
 foreach ($_FILES as $a_file){
  if($a_file['error']!=UPLOAD_ERR_NO_FILE) {
   try{
    $fileName = $upload->upload($a_file,$cfg['path']['root'].'upfile/'.$this_config['originalPath'], 1);
    $f_path['url'] = $this_config['originalPath'].$fileName;
    $f_path['name'] = $a_file['name'];
    $attach_file[] = $f_path;
    if(in_array(strtolower(FileSystem::fileExt($f_path['name'])),array('gif','jpeg','jpg','png')) && !$this_config['noResize']){
     //先縮減到指定大小
     $image = new Image($cfg['path']['root'].'upfile/'.$this_config['originalPath'].$fileName);
     $image->resizeImage($this_config['width'],$this_config['height'],$this_config['resizeType']);
     $image->save();
     //加浮水印
     if($this_config['watermark']){
      $image = new Image($cfg['path']['root'].'upfile/'.$this_config['originalPath'].$fileName);
      $image->waterMark($this_config['watermarkPic'],$this_config['watermarkPos']);
      $image->save();
     }
     //如果需要再產生縮圖
     if($this_config['thumb']){
      $image = new Image($cfg['path']['root'].'upfile/'.$this_config['originalPath'].$fileName);
      $image->resizeImage($this_config['thumbWidth'],$this_config['thumbHeight'],$this_config['thumbResizeType']);
      if($this_config['originalPath']==$this_config['thumbDir']){
       //防止儲存目錄相同時覆蓋原有的圖片,不儲存縮圖直接設定 thumb 屬性為空白
       $image->save(2,$cfg['path']['root'].'upfile/'.$this_config['thumbDir'],'_thumb');
       $thumb_path = $this_config['thumbDir'].FileSystem::getBasicName($fileName, false).'_thumb'.FileSystem::fileExt($fileName, true);
      }else{
       $image->save(1,$cfg['path']['root'].'upfile/'.$this_config['thumbDir']);
       $thumb_path = $this_config['thumbDir'].$fileName;
      }
     }
    }
    //回傳參數
    echo "<script>
     var parentForm;
     if(window.opener){
      parentForm = window.opener;
     }else{
      parentForm = window.parent;
     }
     parentForm.".$js_func."('".$f_path['url']."','".$f_path['name']."','".$thumb_path."');
    </script>";
    
   }catch(Exception $e){
    $page->back( $e->getMessage());
   }
  }else{
   echo "<script>
     alert('請先瀏覽檔案後點擊上傳php教程');
     history.back();
   </script>";
   exit;
  }
  echo "<script>
   /*if(window.opener){
    window.close();
   }else{
    history.back();
   }*/
   history.back();
  </script>";
 }
 echo '</body>';
 echo '</html>';
}elseif($action=="form"){
 echo '<html>';
 echo '<head>';
 echo '<title>上傳檔案</title>';
 echo "<meta http-equiv="content-type" content="text/html; charset=gb2312">";
 echo '</head>';
 echo "<body leftmargin="0" topmargin="0">";
 echo "<table cellpadding="2" cellspacing="1" border="0" height="100%" align="left">";
 echo "<form action='upload.php?action=doupload&to=".$to."' method='post' enctype='multipart/form-data'>";
 echo "<tr ><td  valign='middle'>";
 echo "<input type='file' name='uploadfile'>";
 echo "<input name='submit' type='submit' value='上傳'>";
 echo "</td></tr>";
 echo "</form>";
 echo "</table";
 echo "</body>";
 echo '</html>';
}
?>

聯繫我們

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