Home >Backend Development >PHP Tutorial >PHP example-thinkphp implements attachment upload function

PHP example-thinkphp implements attachment upload function

微波
微波Original
2017-06-28 13:00:481324browse

This article mainly introduces thinkphp to implement the attachment upload function in detail. It has certain reference value. Interested friends can refer to it.

The examples in this article are shared with you. The specific code for uploading attachments in thinkphp is provided for your reference. The specific content is as follows

First you need to make a directory for picturesto be uploaded, and then use the uploadOne method to save it. Sometimes it is necessaryThumbnail, such as the cover image of the article

$config=array(
  'rootPath' => './Application/Public/',//根路径
  'savePath' => 'upload/',
     );
$upload=new \Think\Upload($config);
$z=$upload->uploadOne($_FILES['goods_img']);
 
if(!$z){
    show_bug($upload->getError());
 }else{
      //$z返回的是存储信息
      //只需要把目录和图片名拼接起来就是图片的url
      $url=$z['savepath'].$z['savename'];
 
      //缩略图
      $image=new \Think\Image();
      //用open()打开图像资源,通过路径名找到图像
   $srcimg=$upload->rootPath.$url;
 
      $image->open($srcimg); 
      //按比例缩小图片,长宽不能超出150    
   $image->thumb(150,150);
      //给新图片路径
      $smallimg=$upload->rootPath.$z['savepath'].'small_'.$z['savename'];
      
      //保存图片
      $image->save($smallimg);
}

The above is the detailed content of PHP example-thinkphp implements attachment upload function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn