How to run php script in png/jpg extension

2019-02-19 22:35发布

问题:

How can i edit .htaccess file to make client run php script, but in png/jpg extension. Soo... User will visit url

http://example.com/script.png and he will see "test"(php script will return this value), not image. That's what i already did: .htaccess file:

AddType application/x-httpd-php .png

script.png file:

<?php
header("Content-type: image/png");
echo 'test';
?>

What is wrong with this code? Why it doesn't work? Browser tries to download the image :/

回答1:

Add the following line to your .htaccess file

AddHandler application/x-httpd-php .png

This tells the server to process all files ending with .png as .php.