将一些 PHP 作为 PNG 文件提供
Serve some PHP as a PNG file
我目前正在尝试让一个小脚本在我的服务器上运行,重点是在某个地址提供随机 PNG,例如 https://domain.me/image/。
在 /image/ 中,我有这个 index.php,它可以很好地完成工作并输出我想要的内容:
<?php
$max = 30;
$image = rand(1, $max);
$name = '/var/www/image/src/'.$image.'.png';
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
?>
(我的 PNG 就像你猜的那样存储在 /image/src/ 中)
我现在正在尝试 运行 有人调用 https://domain.me/image/script.png 时的脚本(有些网站要求 URL 末尾有 PNG 扩展名),但实际上不能弄清楚如何进行。
如果您是 运行 一个 LAMP 服务器,您可以使用 url 重写使其工作:http://httpd.apache.org/docs/2.0/misc/rewriteguide.html.
类似于这个问题:.htaccess rewrite image file to php script
如果您使用的是 apache,您可以使用 htaccess 文件来重定向您的请求:
RewriteEngine On
RewriteRule script.png index.php
应该从 index.png 重定向到 index.php
我目前正在尝试让一个小脚本在我的服务器上运行,重点是在某个地址提供随机 PNG,例如 https://domain.me/image/。
在 /image/ 中,我有这个 index.php,它可以很好地完成工作并输出我想要的内容:
<?php
$max = 30;
$image = rand(1, $max);
$name = '/var/www/image/src/'.$image.'.png';
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
?>
(我的 PNG 就像你猜的那样存储在 /image/src/ 中)
我现在正在尝试 运行 有人调用 https://domain.me/image/script.png 时的脚本(有些网站要求 URL 末尾有 PNG 扩展名),但实际上不能弄清楚如何进行。
如果您是 运行 一个 LAMP 服务器,您可以使用 url 重写使其工作:http://httpd.apache.org/docs/2.0/misc/rewriteguide.html.
类似于这个问题:.htaccess rewrite image file to php script
如果您使用的是 apache,您可以使用 htaccess 文件来重定向您的请求:
RewriteEngine On
RewriteRule script.png index.php
应该从 index.png 重定向到 index.php