PHP Main - All your PHP Resource needs!
In this tutorial, I will demonstrate how we can retrieve the contents of different file types, change the content types in the header output and output the content as the selected mime type.
Note that these processes shown here will work in LOTS of circumstances. I've seen it done with mp3, wav, jpeg, gif, png, pdf, all sorts of files! This tutorial will show only the simplest of methods while outputting content of different types but I will explain what the use of this could be.
Let's say you have an image hosting company. You want to determine how many of your visitors are viewing jpegs, gifs, etc. Well, you can easily manage this by controlling image views with a database.
Let's say you setup a RewriteRule in .htaccess to foward files with a .jpg, .jpeg, and .gif extension to a .php file. While it seems absurd at this point, you can easily manage it later. You could setup the fowarding to send the requested file through a $_GET variable to the PHP file.
Once you have the file you wish to retrieve, you can store the path to file in a variable, or several other approaches are also possible. Of course, if you were going to be doing this, I would recommend a security enhanced method of retrieve these files to ensure that secure information isn't exploited through your innocent picture retreiving script.
Let's say you have the requested file in a URL variable named "pic". Self explanitory enough, right?
Let's look at some code:
// viewimage.php - This page will output the actual image
$path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/pictures/' . $_GET['pic'];
$contentType = ( preg_match('#\.gif$#i', $path) ) ? 'gif' : 'jpeg';
$pictureContents = file_get_contents($path);
header('Content-type: image/' . $contentType);
Now, it may seem a bit confusing, but I'm getting ready to explain it. Firstly, we store the remote path to the image on the server, which is (in this example) stored in the $path variable. Like I said previously, I wouldn't recommend doing it this simple because someone could easily exploit you're script!
Once we have the file name and path stored, we can assume that the file extension will be either .jpg, .jpeg, or .gif because I specified above those were the only file types accepted/monitored for activity in this imaginary site.
If you were accepting other file types, you would, of course have to do a more comprehensive search as to the actual type of the file, but we just speed through those checks in this example for brievity. And after all, it is just an example. ;)
Once we have determined the actual type of the image, it's time to get the contents of the file. The contents of an image file are not human readable, so don't attempt to manually edit the image contents or experiment with them, because you'll just run around in endless circles. If you're really curious, here is some of the output caused by the image attached below.
žë–»ÙxÏ±ŸOÓ¼<Z5UD®<- MŒÑàƒ IÑ¼«/}ž“¬;Ó=õ*¹üˆÙÔêdÚ??[ 3Ó. >«¸ÕZë9?®¦`ÓQ>†·D“¸úm
If you can interpret that, I'll give you $1.
Now that we have the content type of the image, the image contents, and we're ready to output it, there is one important modification that needs to take place. If we don't modify the headers of the page, it will assume default properties and attempt to output the contents as HTML and plain text. Well, we of course don't want the mumble-jumble above being displayed to our users, so we modify how the page is executed by the browser.
We can change how the output is rendered through the Content-type: modification to the page headers controlled in the header() function. You can see our actual Content-type: if you use logic to see what would be outputted to the header. In the situation of a file extension that is anything other than .gif, the following code will be sent to the header() function.
If a gif is encountered, it needs to be handled like so:
Now, the only step left to do is output the image. You can make a simple call to the echo (or print if you prefer) construct and have the image right in front of you!
Tada! That's all there is to using PHP to output a multitude of file types.