Have .PNG File Run As .PHP File

Kevin

Code Monkey
Staff member
For a XenForo project I am working on, that dynamically generates a png image, I needed to have a specified png file (shown using a standard img tag) to behind-the-scenes point instead to a php file to execute the code.

At least with an Apache server the solution is quite simple. (y)

In the folder where your php file resides, create an .htaccess file, or modify it if you already have, and add this entry. Just update the "file.png" value to the be the name of the png image that you will be showing to the user and the "file.php" value to the actual name of the script that you want to run.
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^file.png$  file.php [L]
</IfModule>
This method is generic so you can do this for just about any file that you wish to swap instead of just a png & php file. You're just doing a command to tell Apache that "If somebody requests file X then serve file Y instead." during the page load.
 
Back
Top