我在简单的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;
我已经尝试了所有的东西,似乎在圈子里走来走去。任何建议都非常受欢迎。
答案 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();