【问题标题】:Parsing PNG as PHP via htaccess works only on local server but not on webserver通过 htaccess 将 PNG 解析为 PHP 仅适用于本地服务器,但不适用于 Web 服务器
【发布时间】:2012-10-07 02:53:29
【问题描述】:

我在 PHP 中创建了一个动态 PNG 图片。为了使用 PNG 扩展,我创建了一个包含以下内容的 .htaccess-File:

AddType application/x-httpd-php .png

在我的本地 XAMPP 服务器上,一切正常,但是在将文件上传到网络服务器后,它就不再工作了。当我访问该文件时,会显示该文件的 PHP 代码。

这些是来自两台服务器的 2 个不同的答案:

本地服务器:

HTTP/1.1 200 OK
Date: Tue, 16 Oct 2012 21:51:58 GMT
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
Last-Modified: Sat, 07 Feb 2009 11:47:04 GMT
Etag: "9000000024598-1e66-46252b23c9e00"
Accept-Ranges: bytes
Content-Length: 7782
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: image/x-icon

网络服务器:

HTTP/1.1 200 OK
Date: Tue, 16 Oct 2012 21:55:17 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r
Last-Modified: Tue, 16 Oct 2012 21:21:56 GMT
Etag: "db0f1b0-afc-4cc33be6befa5"
Accept-Ranges: bytes
Content-Length: 2812
Keep-Alive: timeout=3, max=100
Connection: Keep-Alive
Content-Type: application/x-httpd-php

所以文件没有被解析。我真的不知道从哪里开始。

【问题讨论】:

    标签: php .htaccess parsing png image


    【解决方案1】:

    我认为您最好将.htaccess 与 RewriteEngine 一起使用,如下所示:

    RewriteEngine On
    RewriteBase /directory/to/dynamic_image
    RewriteRule ^logo\.png$ logo.php
    

    或者对于所有 .php 文件:

    RewriteEngine On
    RewriteBase /directory/to/dynamic_image
    RewriteRule ^(.+)\.png$ $1.php
    

    【讨论】:

    • 谢谢你,完美!但我仍然想知道为什么我的解决方案只能在一台服务器上运行。
    • 你可以看看here。根据您的虚拟主机,某些.htaccess 指令是否允许。
    【解决方案2】:

    在您的 php 文件中将标题设置为 png 图像(在任何输出到页面之前):

    <?php
    header('Content-Type: image/png');
    // image generating code here.......
    ?>
    

    这消除了对 .htaccess 条目的需要

    【讨论】:

    • 我已经这样做了。文件无论如何都不会被解析。也许我在帖子中没有说清楚,但是当我访问网络服务器上的文件时,它会显示文件的源代码
    猜你喜欢
    • 1970-01-01
    • 2017-11-09
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2011-05-24
    相关资源
    最近更新 更多