php - file_get_contents()为空但内容存在

时间:2015-01-30 21:23:26

标签: php

我在简单的PHP脚本中生成基于数据uri的图像。在图像标记内使用时,文件的路径可以正常工作。我正在尝试使用php zip下载此图像。由于内容为空,它目前失败。

工作正常:

$image2 = "http://cdn.screenrant.com/wp-content/uploads/Star-Wars-Logo-Art.jpg";
echo file_get_contents( $image2 ); // returns content to screen

失败:

$image2 = "/myPath/myPath1/script.php";
echo file_get_contents( $image2 ); // nothing displayed

如果我将此路径放在屏幕上的图像标记内,那就没问题了

<img src="/myPath/myPath1/script.php" />

script.php用于生成图像的脚本

$imgstr = "data:image/jpeg;base64,/9j/........... rest of string";
if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) {
die("error");
}

// Decode the data
$content = base64_decode($matches[2]);

// Output the correct HTTP headers
header('Content-Type: '.$matches[1]);
//header("Content-Type: image/jpeg"); // tried this made no difference

// Output the actual image data
echo $content;

我已经尝试了所有的东西,似乎在圈子里走来走去。任何建议都非常受欢迎。

1 个答案:

答案 0 :(得分:1)

<img src="/myPath/myPath1/script.php" />

HTML标记,导致对应的文件的执行/myPath/myPath1/script.php查询字符串。

该文件可以位于服务器上的任何位置,具体取决于您的apache / nginx和.htaccess配置。

另一方面,

$image2 = "/myPath/myPath1/script.php";
echo file_get_contents( $image2 )

尝试阅读该特定路径上位于内容

如果要执行该文件,并且知道它的路径:

$image2 = "/myPath/myPath1/script.php";
ob_start();
include $image2;
$contents=ob_get_clean();