• php 上传图片,无刷新上传,支持多图上传,远程图片上传


     1 <html>
     2 <head>
     3     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     4     <title>无标题文档</title>
     5     <script type="text/javascript">
     6         function startUpload() {
     7             // document.getElementById('processing').innerHTML = 'loding...';
     8             return true;
     9         }
    10         function stopUpload(rel){
    11             var rel = eval('(' + rel + ')');
    12             document.getElementById('processing').innerHTML = document.getElementById('processing').innerHTML + '<img src="'+rel.thumbMap+'" alt="">';
    13         }
    14     </script>
    15 </head>
    16 
    17 <body style="font-size:12px;">
    18 <div id="processing"></div>
    19 
    20 <form action="upload.php" method="post" enctype="multipart/form-data" onsubmit="startUpload();" target="form-target" name="form1">
    21     本地图片:<input type="file" name="file"><br>
    22     远程图片:<input type="text" name="url"><br>
    23     <input type="submit" name="Submit" value="提交">
    24     <input name="scan" type="hidden" id="up" value="true">
    25 </form>
    26 <iframe style="0; height:0; border:0;" name="form-target"></iframe>
    27 <b /><br/>
    28 </body>
    29 </html>
      1 <?php
      2 class ieb_upload
      3 {
      4     public $FormName; //文件域名称
      5     public $Directroy; //上传至目录
      6     public $MaxSize; //最大上传大小
      7     public $CanUpload = true; //是否可以上传
      8     public $doUpFile = ''; //上传的文件名
      9     public $sm_File = ''; //缩略图名称
     10     public $Error = 0; //错误参数
     11 
     12     // 初始化(1024*2)*1024=2097152 就是 2M
     13     public function ieb_upload($FormName = '', $dirPath = '', $maxSize = 2097152) {
     14         // 初始化各种参数
     15         $this->FormName = $FormName;
     16         $this->MaxSize = $maxSize;
     17         if ($this->FormName == '') {
     18             $this->CanUpload = false;
     19             $this->Error = 1;
     20             return;
     21         }
     22         $this->Directroy = $dirPath."/";
     23     }
     24 
     25     //获取远程图片
     26     public function GrabImage($url) {
     27         if($url=="") return false;
     28 
     29         $ext=strrchr($url,".");
     30         if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") { return false; }
     31         //echo $filename=date("YmdHis").$ext;
     32         $fileName = $this->Directroy.$this->newName().$ext; //主图地址
     33         $this->createDir(dirname($fileName));
     34 
     35         ob_start();
     36         readfile($url);
     37         $img = ob_get_contents();
     38         ob_end_clean();
     39         $size = strlen($img);
     40         $fp2 = @fopen($fileName, "a");
     41         fwrite($fp2,$img);
     42         fclose($fp2);
     43 
     44         $this->doUpFile = $fileName;
     45 
     46         chmod($this->sm_File.$fileName, 0777);
     47         return true;
     48     }
     49 
     50     // 上传文件
     51     public function upload() {
     52         if ($this->CanUpload) {
     53             if ($_FILES[$this->FormName]['size'] == 0) {
     54                 $this->Error = 3;
     55                 $this->CanUpload = false;
     56                 return $this->Error;
     57             }
     58 
     59             $fileName =$this->Directroy.$this->newName().".".$this->getExt(); //主图地址
     60             $this->createDir(dirname($fileName));
     61             $doUpload = @copy($_FILES[$this->FormName]['tmp_name'], $fileName);
     62 
     63             if ($doUpload) {
     64                 $this->doUpFile = $fileName;
     65                 chmod($this->sm_File.$fileName, 0777);
     66                 return true;
     67             } else {
     68                 $this->Error = 4;
     69                 return $this->Error;
     70             }
     71         }
     72     }
     73 
     74     // 创建图片缩略图
     75     public function thumb($dscChar = '', $width = 150, $height = 150) {
     76         if ($this->CanUpload && $this->doUpFile != '') {
     77 
     78             if ($dscChar == '')
     79                 $dscChar = 'sm_';
     80 
     81             $data = getimagesize($this->doUpFile, $info);
     82 
     83             switch ($data[2]) {
     84                 case 1:
     85                     $this->sm_File = $this->doUpFile.$dscChar.".gif";
     86                     $im = @imagecreatefromgif($this->doUpFile);
     87                     break;
     88                 case 2:
     89                     $this->sm_File = $this->doUpFile.$dscChar.".jpg";
     90                     $im = @imagecreatefromjpeg($this->doUpFile);
     91                     break;
     92                 case 3:
     93                     $this->sm_File = $this->doUpFile.$dscChar.".png";
     94                     $im = @imagecreatefrompng($this->doUpFile);
     95                     break;
     96             }
     97 
     98             $srcW = imagesx($im);
     99             $srcH = imagesy($im);
    100             $ni = imagecreatetruecolor($width, $height);
    101             imagecopyresized($ni, $im, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
    102             $cr = imagejpeg($ni, $this->sm_File);
    103             chmod($this->sm_File, 0777);
    104 
    105             if ($cr) {
    106                 return true;
    107             } else {
    108                 $this->Error = 5;
    109                 return $this->Error;
    110             }
    111         }
    112     }
    113 
    114     // 检查文件是否存在
    115     public function scanFile() {
    116         if ($this->CanUpload) {
    117             $scan = is_readable($_FILES[$this->FormName]['name']);
    118             if ($scan)
    119                 $this->Error = 2;
    120             return $scan;
    121         }
    122     }
    123 
    124     // 获取文件大小
    125     public function getSize($format = 'B') {
    126         if ($this->CanUpload) {
    127             if ($_FILES[$this->FormName]['size'] == 0) {
    128                 $this->Error = 3;
    129                 $this->CanUpload = false;
    130             }
    131             switch ($format) {
    132                 case 'B':return $_FILES[$this->FormName]['size'];break;
    133                 case 'K':return ($_FILES[$this->FormName]['size']) / (1024);break;
    134                 case 'M':return ($_FILES[$this->FormName]['size']) / (1024 * 1024);break;
    135             }
    136         }
    137     }
    138 
    139     // 获取文件类型
    140     public function getExt() {
    141         if ($this->CanUpload){
    142             $ext = $_FILES[$this->FormName]['name'];
    143             $extStr = explode('.', $ext);
    144             $count = count($extStr)-1;
    145             return $extStr[$count];
    146         }
    147     }
    148     // 获取文件名称
    149     public function getName() {
    150         if ($this->CanUpload)
    151             return $_FILES[$this->FormName]['name'];
    152     }
    153 
    154     //创建文件夹
    155     public function createDir($path){
    156         if (!file_exists($path)){
    157             $this->createDir(dirname($path));
    158             mkdir($path, 0777);
    159         }
    160     }
    161 
    162     // 新建文件名
    163     public function newName() {
    164         if ($this->CanUpload) {
    165             //$FullName = $_FILES[$this->FormName]['name'];
    166             //$extStr = explode('.', $FullName);
    167             //$count = count($extStr)-1;
    168             //$ext = $extStr[$count];
    169             //return date('YmdHis').rand(0, 9).'.'.$ext;
    170             return date('YmdHis').rand(0, 9);
    171         }
    172     }
    173 
    174     // 显示错误参数
    175     public function Err() {
    176         return $this->Error;
    177     }
    178 
    179     // 上传后的文件名
    180     public function UpFile() {
    181         if ($this->doUpFile != '')
    182             return $this->doUpFile;
    183         else
    184             $this->Error = 16;
    185     }
    186 
    187     // 上传文件的路径
    188     public function filePath() {
    189         if ($this->doUpFile != '')
    190             return $this->sm_File.$this->doUpFile;
    191         else
    192             $this->Error = 26;
    193     }
    194 
    195     // 缩略图文件名称
    196     public function thumbMap() {
    197         if ($this->sm_File != '')
    198             return $this->sm_File;
    199         else
    200             $this->Error = 36;
    201     }
    202 
    203     // 显示版本信息
    204     public function ieb_version() {
    205         return 'IEB_UPLOAD CLASS Ver 1.1';
    206     }
    207 }
    208 
    209 if (isset($_REQUEST['scan']))
    210 {
    211     // 声明一个上传类
    212     $upfos = new ieb_upload('file', 'D:/wamp64/www');
    213     //远程图片
    214     if($_REQUEST['url']){
    215         $upfos->GrabImage($_REQUEST['url']);
    216     }else{
    217         $upfos->upload();//上传主图
    218     }
    219     $upfos->thumb();  //生成缩略图
    220     //返回数组
    221 
    222     $re['name'] = $upfos->getName();//文件名
    223     $re['ext'] = $upfos->getExt(); //扩展名
    224     $re['size'] = $upfos->getSize(); //扩展名
    225 
    226     $re['newName'] = $upfos->UpFile(); //新文件名
    227     $re['filePath'] = $upfos->filePath(); //文件路径
    228     $re['thumbMap'] = dirname($upfos->thumbMap()); //文件路径
    229 }
    230 ?>
    231 <script type="text/javascript">
    232     var re = '<?php echo json_encode($re); ?>';
    233     window.top.window.stopUpload(re);
    234 </script>
  • 相关阅读:
    QTreeWidget创建
    Qt QTreeWidget节点的添加+双击响应+删除详解(转)
    Qt QTreeWidget 树形结构实现(转)
    QMessageBox类学习:
    QAction类详解:
    Qt事件和信号的区别 .
    Qt消息机制和事件(二)
    Qt消息机制和事件(一)
    初步开始学习图
    图中最短路径算法(Dijkstra算法)(转)
  • 原文地址:https://www.cnblogs.com/spectrelb/p/6861816.html
Copyright © 2020-2023  润新知