2012-10-16 91 views
2

我已经在PHP中创建了一个动态PNG图片。为了使用PNG扩展我创建了一个的.htaccess-文件,内容如下:通过htaccess解析PNG为PHP只适用于本地服务器,但不适用于网络服务器

AddType application/x-httpd-php .png 

在我的本地XAMPP服务器一切都运行完美,但在上传一个网络服务器中的文件后,它不” T工作了。当我访问文件时,会显示文件的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 

于是莫名其妙的文件没能坐解析。我真的不知道从哪里开始。

回答

5

我想你最好使用.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 
+0

谢谢你,完美的作品!但我仍然想知道为什么我的解决方案只能在一台服务器上运行。 – Michael

+1

你可以看看[这里](http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/)。根据您的虚拟主机,某些'.htaccess'指令是允许的或不允许的。 –

2
在你的PHP文件

设置页眉为PNG图像(在任何输出到页面之前):

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

这消除了需要.htaccess条目

+1

我已经做到了。无论如何,文件都不会被解析。也许我没有在我的文章中说清楚,但是当我访问网络服务器上的文件时,它显示文件的SOURCE CODE – Michael

相关问题