Left Quote    It is possible to store the mind with a million facts and still be entirely uneducated.
- Alec Bourne    
Right Quote
[login] | [Register]

Uploading Image Files with PHP

by: bs0d
Page: 3 of 5
(View All)

Check File Size (in bytes)

Now that we have accessed the file selected to be uploaded and obtained the size, the next step is to begin filtering the file through your limitations. The numerical limitations we specified above (size, height and width),. Before checking the height and width, lets first check that the file size is within the parameters set. If you recall, we set a maximum file size as a part of the form (MAX_FILE_SIZE), as well as the $max_size variable. We will check the size of the file against both of the variables specified to ensure that the file is within the specified limits. See code below for details:

As you can see, the code checks if the uploaded file is greater than MAX_FILE_SIZE or greater than the $max_size variable. If it is, the condition will return TRUE, and the die() function will be executed that kills the script and displays the message within.

Check File Extension

The next part in the process of filtering the file selected for upload is to determine if the file carries an image extension. For this article, the code is written to only allow JPG or PNG files. Using the Explode() function, the name of the image (including the extension) is stored into an array. Explode splits the variable at the specified character. In the code, the variable is split at "." which precedes the extension. Next, the count() function checks to see how many elements are in the array, and then the last element of the array (which should be the extension name) is stored in the $ext variable. From this point, the extension has been derived from the file name, and the IF statement check to see if it is a JPG or PNG. See the full code below:

Check and Double Check File Type

Now that the file is within the size you deem appropriate to be uploaded, and carries an image extension, we will filter by file type to verify it is an image. Several methods exist for determining the type of file uploaded. 1.) We can check the type based on what the browser sent, or 2.) Check the MIME type from the GD Library getimagesize() function. This method requires the GD Library to be installed on the host server for your site. #2 would be the preferred method, since the file type determined by the browser can be faked. But in the case you do not have the GD Library installed, I will show the code to do both. In fact, it might not be a bad idea to just run both just to be safe.

The code below is to allow only JPG or PNG images. I believe older versions of Internet Explorer recognized JPG files as image/jpeg. But with the new version, like Firefox, IE recognizes JPG files as images/pjpeg. So you can check either or to be safe. As for PNG files, I believe image/png will work just fine, and image/x-png will also be recognized.

Check Image Size (Height and Width)

By reaching this point of the script, it can be assumed that an image was selected for upload, as it has passed through the conditions for extension name, file type and size. The last condition the image must pass is verification of height and width. To do this, the variables specified at the beginning of the script can be utilized, rather than specifying a static number. This way, if you change your mind, you can just change the value of the variable, instead of searching through the code to make multiple edits in possibly multiple locations.

Under the "Accessing the File" portion of this article, we stored size information about the uploaded file into the $info variable. The width of the file is stored in: $info[0], and height is stored in: $info[1]. A simple condition allows us to compare these values vs. the variables specified. See the code below:

As a part of error handling, if the user uploaded a file that is too large, take the time in the code to display to the user what size of file was uploaded, and what the limit constraints are. This allows the user to make the appropriate change to complete a successful upload on the next attempt.

1  |  2  |  3  |  4  |  5  |  
Next »


  Subject: "Please help with upload script" Date: Mar 25 2008 at 6:05 am    
Hey there, could someone please explain this piece of code a little bit more!

//check the extension.
$array = explode(" .", $_FILES['userf ile']['name' ]);
$nr = count($array);
$ext = $array[$nr-1];< br />

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

8 Total Comments
5 Rating of 5 (1 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope

"" Copyright © 2002-2020; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank