PHP缓存脚本返回破碎的图像


PHP cache script returns broken image

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

    <?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);
    ?>

这工作了一段时间,直到今天。我不知道为什么。它只是返回一个破碎的图像图标。

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

$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。