1.入口文件定义:

  1. define('ROOT',$_SERVER['DOCUMENT_ROOT']);
复制代码

2.公共配置文件代码,保存路径为Public/Uploads,在Public下建Uploads目录

  1. 'UPLOADS'=>array(
  2.         'rootPath' =>'./',
  3.         'maxSize'=>204800,
  4.         'savePath'=>'./Public/Uploads/',
  5.         'exts'=> array('jpg', 'gif', 'png', 'jpeg'),
  6. ),
复制代码

3.控制器里的方法

  1. public function upload(){
  2.         $upload = new \Think\Upload(C('UPLOADS'));
  3.         $info = $upload->upload();  
  4.         if(!$info){     
  5.             $this->error($upload->getError());    
  6.         }else{
  7.             foreach ($info as $file)
  8.             {
  9.                 $path = substr($upload->__get('rootPath'), 1) . substr($file['savepath'], 2) . $file['savename'];
  10.             $path = dirname($_SERVER['SCRIPT_NAME']) . $path;
  11.             $image = new \Think\Image();
  12. $image->open(ROOT.$path);
  13.             $path1= substr($upload->__get('rootPath'), 1) . substr($file['savepath'], 2) . 'thumb'.$file['savename'];
  14.             $thumbPath=dirname($_SERVER['SCRIPT_NAME']) . $path1;
  15.             $image->thumb(100, 100,\Think\Image::IMAGE_THUMB_FILLED)->save(ROOT.$thumbPath);
  16.         }
  17.             echo "<p id='p1'>$path</p>";
  18.             echo "<p id='p2'>$thumbPath</p>";
  19.             $a=<<<path
  20. <script>
  21.     var p1=document.getElementById("p1").innerHTML;
  22.     var p2=document.getElementById("p2").innerHTML;
  23.     var path1=parent.document.getElementById('path1');
  24.     var path2=parent.document.getElementById('path2'); 
  25.     var img=parent.document.getElementById('img'); 
  26.     path1.value=p1;
  27.     path2.value=p2;
  28.     img.src=p2;
  29. </script>
  30. path;
  31.          echo $a;
  32.      }
  33.  }
复制代码

4.html代码

  1. <iframe id='frameFile' name='frameFile' style="display:none;"></iframe>
  2.     <form id='formFile' name='formFile' method="post" action="{:U('模块/控制器/upload')}" target='frameFile' enctype="multipart/form-data">
  3.         <input type="file" name="pic">
  4.         <input type="submit" value="上传">
  5.     </form>
  6.      原始大小图片路径<input  id='path1' type="text"><br>
  7.      缩略图路径<input id='path2' type="text"><br>
  8.      缩略图<img id="img" src="">
复制代码

5.ThinkPHP,tract页面报以下错误
a.Library\Think\Upload.class.php170行对$this->callback未做判断,我在前面加了

  1. if($this->callback)
复制代码

b.Library\Think\Image\Driver\Gd.class.php270行imagecopyresampled($img, $this->img, $posx, $posy, $x, $y, $neww, $newh, $w, $h);参数中$x,$y未定义
我在259行后加了(如果不是0位置开始,自己调整参数值)

  1. $x=$y=0;
复制代码

6.多图上传的保存路径已经做了循环,其他自己完善,主要是js循环.

posted on 2016-10-11 11:31  飘渺的悠远  阅读(287)  评论(0)    收藏  举报