php 经典安全文件上传代码

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

时间: 2024-09-20 11:41:21

php 经典安全文件上传代码的相关文章

PHP文件上传代码用法

  php文件上传代码编写过程 1. 先判断是否上传文件 2. 如果有再来判断上传中是否出错 3. 如果出错,则提示出错信息 4. 如查没出错,再判断文件类型 5. 如果类型符合条件,再判断指定目录中有没有存在该文件 6. 如果没有就把该文件移至指定目录 在php中上传文件必须知道的几个东西 $_FILES['myfile']['name'] 是指被上传文件的名称 $_FILES['myfile']['type'] 是指被上传文件的类型 $_FILES['myfile']['size'] 是指被

java 后台实现文件上传代码

问题描述 java 后台实现文件上传代码 java后台做一个文件上传,前端选择了照片点击上传到后台,后台需要保存到服务器上, 这个该怎么做,能不能贴一贴现成的代码?求解 解决方案 try { ??????????? FileItemFactory factory = new DiskFileItemFactory(); ??????????? ServletFileUpload upload = new ServletFileUpload(factory); ??????????? ??????

ajax+php 无刷新文件上传代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.111cn.net/1999/xhtml"> <head> <meta http-equiv="conte

ajax php多文件上传代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-

php简单实用文件上传代码(1/2)

<?php教程 if($_files['file']){  // ----------------------------------------------------------------------------------------------// // // 说明:文件上传   日期:2004-5-2 // // --------------------------------------------------------------------------------------

php 文件上传代码,限制jpg文件

php 文件上传代码,限制jpg文件 <?php     /* 图片上传类 www.lost63.com原创代码 仅限JPG格式图片 */     class uploadFile     {         var $inputName; //input名称         var $fileName; //文件命名         var $fileProperty; //文件属性         var $fileSize=2097152; //文件大小限制,2M         var

php最简单的文件上传代码

php最简单的文件上传代码 <?php    include("top.txt");    include("scripts.txt");    if(isset($_FILES['ufile']['name'])){        echo "Uploading: ".$_FILES['ufile']['name']."<br>";        $tmpName = $_FILES['ufile']['t

php 入门型-文件上传代码

php 入门型-文件上传代码 <?php    if(!empty($_FILES["uploadImage"])) {     // get file name   $filename = basename($_FILES['uploadImage']['name']);      // get extension     $ext = substr($filename, strrpos($filename, '.') + 1);          // check for j

asp.net fileupload文件上传代码

asp教程.net fileupload文件上传代码 <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css教程" href="history/his