PHP文件上传程序代码,精彩安全文件上传代码

作者: 关于计算机  发布:2019-09-28

<?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];

听闻jQuery Ajax PHP文件上传程序代码 那是一款基于jQuery的PHP多文件上传程序实例,利用了ajaxmultifileupoad插件来实例文件上传哦,带上传进度条的。

 /*  推断特殊字符 */
 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;
  }
 }

基于jquery ajax php教程文件上传程序代码
那是一款基于jquery的php多文件上传程序实例,利用了ajaxmultifileupoad插件来实例文件上传哦,带上传进程条的。

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

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
        ";
<html xmlns=";
<head>
    <meta http-equiv="content-type" content="text/html; charset=gbk"/>
    <title>ajax file upload download by ;

 <link href="css教程/style.css" rel="stylesheet" type="text/css"/>
 <script type="text/网页特效" src="js/jquery-1.2.6.min.js"></script>
 <script type="text/javascript" src="js/jquery.livequery.min.js"></script>
 <script type="text/javascript" src="js/jquery.ajaxmultifileupload.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){ 
  $("#maincol").ajaxmultifileupload();
 }); 
 </script>
</head>

<body>
 <div id="maincol"></div>
</body>
</html>

//php代码

<?php

function _resize($_w, $max_w, $_h, $max_h) {
 global $new_w, $new_h;
 
 if ($_w>$max_w || $_h>$max_h) {
  $x_ratio = $max_w / $_w;
  $y_ratio = $max_h / $_h;
  if (($x_ratio * $_h) < $max_h) {
   $new_h = ceil($x_ratio * $_h);
   $new_w =$max_w;
  }
  else {
   $new_w = ceil($y_ratio * $_w);
   $new_h=$max_h;
  }
 }
 else {
  $new_w = $_w;
  $new_h=$_h;
 }
}

if ($_files) {
 foreach ($_files as $f) {
   // get actual image type
   $ext = strtolower(str_replace("image/", "", image_type_to_mime_type(exif_imagetype($f['tmp_name']))));
   // get image type from file extension
   $ext_file = strtolower(str_replace("image/", "", $f['type']));
   // adjustment for ie mime types
   $ext_adjust = array("pjpeg" => "jpeg", "x-png" => "png");
   $ext_file = (array_key_exists($ext_file, $ext_adjust)) ? $ext_adjust[$ext_file] : $ext_file;
   
   // check if file actual and ext file types match
   if ( ($ext_file != $ext) )  {
    $ps教程t = array("problem" => array("name" =>$f['name'], "ext" => $ext, "ext_actual" => $ext_file));
   }
   else {
    
    $stat = stat($f['tmp_name']);
    $renamed = ($_post['mode']=='demo') ? $stat[9].'.'. substr(strrchr($f['name'], "."),1) : $f['name'];
    
    // set path for upload directory
    $f['_upload'] = $_server['document_root'] . $_post['upload'] . $renamed;
    // set path for image thumbnail directory
    $f['_thumb'] = $_server['document_root'] . $_post['thumb'] . $renamed;
    $f['src'] = "thumb/" . $renamed;
    
    chmod($f['tmp_name'], 0755);
    
    // get image size - requires gd library
    list($width, $height) = getimagesize($f['tmp_name']);
      
    $max_width = 120;
    $max_height = 60;
    _resize($width, $max_width, $height, $max_height);

    // create thumbnail with image_class (files included in download)
    include_once('imageclass/image_class.inc');
    set_time_limit(140);
    try{
     // create new image from posted $_files
     $image=& new image($f['tmp_name']);
     $image->resize(round($new_w),round($new_h));
     // save image to thumbnail directory
     $image->saveimage($f['_thumb'],$image->getimagedata());
     $image->clean();
     // move actual posted $_files to upload directory
     $moved = move_uploaded_file($f['tmp_name'], $f['_upload']);
     if ($moved) $pst = array ("img" => array("alt" => $f['_thumb'], "src" => $f['src'], "name" => $f['name'],  "rename" => $renamed, "width" => $new_w, "height" => $new_h));
    }
    catch(exception $e){
        $pst = array ("problem" => array("name" =>$f['name'], "error" => $e->__tostring()) );
    }    
   }
   echo "var pst = " . json_encode($pst);
 }
}
else if ($_post['deletefile']) {
 $_file = ($_post['mode']=='demo') ? urldecode($_post['deletefile']) : urldecode($_post['origname']);
 // delete tmp file
 $_unlink_upload = unlink($_server['document_root'] . $_post['upload'] . $_file);
 // delete thumb file
 $_unlink_thumb = unlink($_server['document_root'] . $_post['thumb']. $_file);
 
 if ($_unlink_thumb && $_unlink_upload) echo "file <span>" . $_post['origname'] . "</span> deleted.";
 else echo "problem deleting file " . $_file . ".";
}

   
/*
文本下载地址


*/

Ajax PHP文件上传程序代码那是一款基于jQuery的PHP多文件上传程序实例,利用了ajaxmultifileupoad插件来实例文件上传哦,带上传进程条...

本文由贝博体育app发布于关于计算机,转载请注明出处:PHP文件上传程序代码,精彩安全文件上传代码

关键词: