Home >Backend Development >PHP Tutorial >ThinkPHP3.0 thumbnail cannot be saved to subdirectory solution summary_PHP tutorial

ThinkPHP3.0 thumbnail cannot be saved to subdirectory solution summary_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:56:211013browse

Thumbnails cannot be saved to subdirectories after uploading files in THINKPHP3.0. This is because the getSubName() function in the upload class UploadFile.class.php can only create a subdirectory of the original image but not a subdirectory of the thumbnail. , it can be said to be a BUG.

Solution 1 (the official solution provided by ThinkPHP, I have not tested it): Upgrade to the latest UploadFile.class.php of ThinkPHP3.1 (https://github.com/liu21st/extend/tree/master/Extend /Library/ORG/Net), replace the original UploadFile.class.php
after downloading

Solution 2: Modify some codes of UploadFile.class.php

This is my own solution, add a thumbnail subdirectory generation function

Step 1>>

Imitate the getSubName() function in UploadFile.class.php to create a getThumbSubName() function

   $dir = date($this->dateFormat,time());    break;
The code is as follows
 代码如下 复制代码

private function getThumbSubName($file) {
  switch($this->subType) {
    case 'date':
      $dir = date($this->dateFormat,time());
      break;
    case 'hash':
    default:
      $name = md5($this->thumbPath);
      $dir = '';
      for($i=0;$i<$this->hashLevel;$i++) {
        $dir .= $name{$i}.'/';
      }
      break;
  }
  if(!is_dir(($this->thumbPath).$dir)) {
    mkdir(($this->thumbPath).$dir);
  }
  return $dir;
}

Copy code

private function getThumbSubName($file) {

switch($this->subType) {
 代码如下 复制代码

$thumbPath = $this->thumbPath?$this->thumbPath.($this->autoSub?$this->getThumbSubName($file).'/':''):$file['savepath'];

​​case 'date':
case 'hash':   default:

   $name = md5($this->thumbPath);

   $dir = '';     $dir .= $name{$i}.'/';    }    break; } ​if(!is_dir(($this->thumbPath).$dir)) {   mkdir(($this->thumbPath).$dir); } ​return $dir; }
Step 2>>
Change line 158 in UploadFile.class.php to
The code is as follows Copy code
$thumbPath = $this->thumbPath?$this->thumbPath.($this->autoSub?$this->getThumbSubName($file).'/':''):$file[ 'savepath']; http://www.bkjia.com/PHPjc/632175.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632175.htmlTechArticleTHINKPHP3.0 After uploading the file, the thumbnail cannot be saved to the subdirectory because of the UploadFile.class.php upload The getSubName() function in the class can only create a subdirectory of the original image but cannot create a thumbnail image...
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