Parsing PNG as PHP via htaccess works only on local server but not on webserver

三世轮回 提交于 2019-12-24 03:15:24

问题


I have created a dynamic PNG-picture in PHP. In order to use the PNG extension I created a .htaccess-File with the following content:

AddType application/x-httpd-php .png

On my local XAMPP server everything works perfect, but after uploading the files on a webserver it doesn' t work anymore. When I access the file, the PHP-Code of the file is displayed.

These are the 2 different answers from both servers:

Local Server:

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

Webserver:

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

So somehow the file doesn' t get parsed. I really don' t know where to start here.


回答1:


I think you'd better use a .htaccess with the RewriteEngine like this :

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

Or for all .php files :

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



回答2:


in your php file set the header as a png image (BEFORE any output to the page):

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

This does away with the need for the .htaccess entry



来源:https://stackoverflow.com/questions/12924314/parsing-png-as-php-via-htaccess-works-only-on-local-server-but-not-on-webserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!