【问题标题】:PHP giving image with throttled bandwitdth and caching?PHP 提供具有节流带宽和缓存的图像?
【发布时间】:2012-02-16 20:06:18
【问题描述】:

Here 是指向文件带宽限制 PHP 脚本示例的链接。我看到我可以做出的改进与我稍后会自己做的无关,但除此之外......你怎么能使用这个脚本来制作另一个脚本,它返回一个限制带宽的图像,但永远缓存图像?图像永远不会改变。

【问题讨论】:

    标签: php image caching bandwidth bandwidth-throttling


    【解决方案1】:

    我认为您所要求的只是修改该脚本以处理图像并缓存内容。应该这样做,等待我可能遇到的任何小错误:

    <?php
    
    $file = "yourimage.jpg"; // file to be send to the client
    $speed = 8.5; // 8,5 kb/s download rate limit
    
    // if $file is coming from get, I would use this to prevent against a nullbyte attack:
    $file = str_replace(chr(0), '', $file);
    
    if (file_exists($file) && is_file($file)) {
        header("Expires: ".gmdate('D, d M Y H:i:s', time()+3600*24*3000).'GMT'); // expires in 3000 days. 
        header("Pragma: cache");
        header("Content-Type: image/jpeg"); // needs to be changed for the file type.
        header("Content-Length: ".filesize($file));
        header("Cache-Control: max-age=" . 3600*24*3000);
    
        flush();
    
        $fd = fopen($file, "r");
        while(!feof($fd)) {
             echo fread($fd, round($speed*1024));
            flush();
            sleep(1);
        }
        fclose ($fd);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 2010-12-08
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      相关资源
      最近更新 更多