【问题标题】:How to delete specific files from timthumb's cache?如何从 timthumb 的缓存中删除特定文件?
【发布时间】:2012-10-16 09:11:40
【问题描述】:

我有一个照片共享应用程序,我使用timthumb 来显示缩略图。当用户删除照片时,我希望缓存的图像也从 timthumb 的缓存目录中删除以节省空间。目前我只能从缓存中删除所有文件,但并不理想。

在给定原始图像的情况下,如何仅从 timthumb 的缓存中删除特定文件?

【问题讨论】:

    标签: php timthumb


    【解决方案1】:

    假设我们要删除本地图片的缓存文件:

    我们首先需要的是 cacheDirectory = './cache'
    二、FILE_CACHE_PREFIX = 'timthumb'
    那么它是内部的(_int_)
    那么有一个 md5 散列:

    • timthumb.php的修改时间
    • 字符'-'
    • timthumb.php 的 inode
    • 图片的修改时间
    • 调用图像时的 $_SERVER ['QUERY_STRING']
    • 'fileCacheVersion' 始终是数字 1

    最后,FILE_CACHE_SUFFIX = '.timthumb.txt'


    例如:

    HTML:

    <img src='timthumb.php?src=images/example.png&w=150'>
    

    PHP:

    $cachefile = 'cache/timthumb_int_'.md5(filemtime('timthumb.php') . '-' . fileinode('timthumb.php') . filemtime("images/example.png"). "src=images/example.png&w=150" . 1) . '.timthumb.txt';
    unlink($cachefile);
    

    【讨论】:

      【解决方案2】:

      我来到了这个解决方案。我必须对timthumb.php 稍作修改 文件,以便我可以包含该文件并从另一个文件实例化该类。

      timthumb.php:

      <?php
      
      // If this file is included from other script, don't start timthumb
      if (basename($_SERVER['SCRIPT_NAME']) == basename(__FILE__)) {
        timthumb::start();
      }
      
      class timthumb {
        ...
        // $cachefile is protected so I create a getter function
        public function getCacheFile() {
          return $this->cachefile;
        }
      }
      

      现在我可以获取给定图像的缓存文件名并将其删除。代码不是很漂亮,但我需要节省空间。

      delete_cache.php:

      <?php
      require 'timthumb.php';
      
      // Fake the query string
      $_SERVER['QUERY_STRING'] = 'src=path/to/src/image.jpg&w=200&h=150';
      parse_str($_SERVER['QUERY_STRING'], $_GET);
      
      // When instantiated, timthumb will generate some properties: 
      // salt, directories, cachefile, etc.
      $t = new timthumb();
      
      // Get the cache file name
      $f = $t->getCacheFile();
      
      // Delete the file
      if (file_exists($f)) {
          unlink($f);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        相关资源
        最近更新 更多