【问题标题】:Serve some PHP as a PNG file提供一些 PHP 作为 PNG 文件
【发布时间】:2015-11-13 16:11:31
【问题描述】:

我目前正在尝试在我的服务器上运行一个小脚本,重点是在某个地址提供随机 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 扩展名),但我真的不知道如何继续。

【问题讨论】:

    标签: php rewrite web redirect


    【解决方案1】:

    如果你使用 apache,你可以使用 htaccess 文件来重定向你的请求:

    RewriteEngine On    
    RewriteRule script.png index.php
    

    应该从 index.png 重定向到 index.php

    【讨论】:

    • 像魅力一样工作。出于某种原因,我正在尝试RewriteRule index.php script.png。谢谢!
    【解决方案2】:

    如果您正在运行 LAMP 服务器,则可以使用 url 重写使其工作:http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

    类似这个问题:.htaccess rewrite image file to php script

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2020-08-21
      • 2022-01-03
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多