Treat PNG file as PHP - php

I want to upload a PHP file to a server under a PNG extension, while still being able to view it in my browser as a PHP file. How can I do that? Thanks.

Use the following in your .htaccess file in the directories where your files are:
<FilesMatch "\.png$">
SetHandler application/x-httpd-php
</FilesMAtch>
http://httpd.apache.org/docs/2.2/mod/mod_mime.html
http://bytes.com/topic/php/answers/745293-force-apache-parse-html-files-php
Note that ALL files with .png will be handled by PHP with this.

You can add AddType application/x-httpd-php .png to an .htaccess file (assuming your host allows you to use .htaccess files), and Apache will send those file off to be parsed by PHP.

Why not use URL Rewriting for this?

You cannot. PHP is server-side code and is not rendered in the browser.

Related

.htaccess php with .gif extension

I know I can write a php script and save it with an image ext. by adding the following to my .htaccess
AddType application/x-httpd-php .gif
But what if I only want files in a certain directory to be treated as such? How would I do that? I'm thinking about making a a footer for my email account, I would like to gather some basic non-intrusive data with it. Would this even work (assuming the client doesn't have images turned off)?
<Files "/thisdirectory">
AddType application/x-httpd-php .gif
</Files>
See here for Apache 2.4 docs.
http://httpd.apache.org/docs/current/mod/core.html

Apache/PHP file type confusion

I have a quick question about changing the file types PHP parses. This website gave this line:
AddType application/x-httpd-php .php
But I'm not clear what file this goes into. Any help would be appreciated - thanks!
That line goes into a file called .htaccess, that changes the Apache server configuration for the folder it is on, and all its subfolders (Unless otherwise specified)
Your server should parse php files with the .php file extension by default though. You could use that to add custom file formats for example.
To parse .mp4 files, like you said in the comments, add to your .htaccess:
AddType application/x-httpd-php .mp4
You need to write that in your .htaccess file.
That makes Apache parse .php files through the PHP interpreter.
You can either add that to your .htaccess or httpd.conf file, depending on what you have access to.

PHP code in HTML without a file extension

I currently have a Movable Type blog where the pages do not have any file extensions...so example.com/entries/this-is-my-entry
I added DefaultType text/html, so Apache knows if there isn't any extension on the end of the file to display as HTML. That works great.
Now the problem is I have PHP code in the HTML. The page won't render the PHP code if there is no extension. However, when I publish the entries with a .html extension....the PHP code works perfectly.
I have AddHandler php5-script .html to tell Apache to display PHP in the HTML files. I'm guessing, if there isn't any file extension, it doesn't know to render the PHP code, because it is expecting files with a .html extension...is there a way to say any file that doesn't have an extension can use PHP?
I'm doing the same implementation for my personal MT blog, with the entries having no file extension. (The theory being that I could switch to other techniques in the future without being hampered by extensions like .html, .php, etc.)
You can accomplish this by setting your default type to be PHP:
DefaultType application/x-httpd-php
All PHP files are interpreted at first as HTML, so this works even for files with no PHP in them.
Maybe you can try using <FileMatch> directive? Something like
<FilesMatch "^[^\.]$">
ForceType application/x-httpd-php
</FilesMatch>
There is nothing directly corresponding to DefaultType for AddHandler, but you can hack around it in a fairly ugly way:
<FilesMatch "^(?!.*\..+)">
SetHandler php5-script
</FilesMatch>

Can I change the accepted extensions by PHP?

I'd like to accept other type of files that contains PHP code. For example, it would be nice to read an .aspx file by PHP as if it were .php.
Add this to your .htaccess file in Apache to make html parse as PHP:
AddType application/x-httpd-php .html
You can extrapolate what you need to do from there. :-)
Use this htaccess rule:
AddType application/x-httpd-php .php .aspx
Yes, this is done in your Apache configuration. You tell apache what kind of files you want it to send to the PHP engine. You can do this configuration the same way you do other apache configuration - in the main config file, per site, or in .htaccess files.

Can we include php file in html file?

How can we include a php file into a html file?
I know we can do this with:
<AddType application/x-httpd-php .html>
but I don't have access to the server. Any other solution which I can include in my website folder or file?
Why not just rename the file to end in PHP? That would be the simplest way to do this.
If you have access to your website folder, you could create a .htaccess file that includes:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .html
</IfModule>
You could alternatively try:
<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch>
Depending on what the code looks like that you want to include, and if you really have no other choice like the ones presented by Druid, you can use one of the following less-than-perfect embedding solutions:
An IFRAME with frameborder="0" pointing to the PHP script (Downside: dynamic resizing according to output size is possible only with JavaScript
An AJAX Snippet that loads the PHP file onLoad and injects the output into a DIV of your choice
A tag pointing to the PHP file. The PHP file must then issue JavaScript commands like "document.write()".
The IFRAME is the most fail-safe in the event the client has no JavaScript. The AJAX Snippet is the most elegant solution.
The correct solution is not renaming it to .php.
It is renaming to .phtml ;)
.phtml is the official file type for crossover files containing PHP and HTML code.

Categories