PHP缓存脚本返回损坏的图像

时间:2013-06-06 14:18:37

标签: php nginx

我正在尝试从外部网站缓存用户外观并将其本地缓存在同一文件夹中。 到目前为止我已经想出了这个:

    <?php
  $figure = $_GET['figure'];
    $action = $_GET['action'];
    $direction = $_GET['direction'];
    $head_direction = $_GET['head_direction'];
    $gesture = $_GET['gesture'];
    $size = $_GET['size'];
    $imgFile = "$figure$action$direction$head_direction$gesture$size";
    $imagesPath =  $imgFile;

    if(!file_exists(($imagesPath))) {

        $otherSiteUrl  = "http://sourcewebsite.com/image/look?figure=$figure&action=$action&direction=$direction&head_direction=$head_direction&gesture=$gesture&size=$size";
        file_put_contents($imagesPath, file_get_contents($otherSiteUrl));
        }

 header("Content-Type: image/png");
        readfile($imagesPath);

    ?>

直到今天还有一段时间。 我不知道为什么。它只返回一个破损的图像图标。

1 个答案:

答案 0 :(得分:0)

首先,可能需要指定保存这些图像文件的目录,而不是正在运行的脚本的当前目录。例如,您可以将它们保存在图像的子文件夹中,因此图像路径将成为;

$imagesPath = dirname(__FILE__) . '/imageCache/' . $imgFile;

这将要求您在脚本所在的目录中创建名为“imageCache”的目录。

接下来,您应该在此目录上设置权限,以允许脚本将文件写入其中。请参阅适用于Windows的http://technet.microsoft.com/en-us/library/bb727008.aspx或适用于unix(mac和linux)的http://www.dartmouth.edu/~rc/help/faq/permissions.html