Search
Left Quote    I start where the last man left off.
- Thomas A. Edison    
Right Quote
 
[login] | [Register]
 

Uploading Image Files with PHP

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

Form Continued


The final part of the form will be the submit button. Once clicked, we will begin determining if a file was selected to upload, and if it is a valid file to upload. The submit button is the same as any other form, nothing changes:

Now that your form has been constructed, we can begin the script. Lets add the IF statement above the form, with the ELSE condition to display the form. Therefore, we will have a closing <? } ?> as the very last line of code. With this added, here is what your code will look like so far:

Remember, the form code listed is just the essentials for it to work; you will build your design around this, or add the elements to an existing form you have.

Beginning the Script


Our first line of code is to determine if the submit button has been pressed. That is expressed by if(isset($_POST['submit'])) . From this point we begin to actually analyze the file selected to be uploaded. Step one is to determine if the user selected a file to be uploaded. Remember, all information about the image will be available in the $_FILES array. In our case, it is: $_FILES['userfile'] . To see all of the information or variables stored in $_FILES, you can use var_dump. If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none. So we will check if the user selected a file for upload by checking the size. See code below:

The IF condition checks if the file size is greater than 1. If it is, then execution of the script continues. All other conditions will be placed inside this IF statement. If not, code would be executed un-necessarily, not to mention errors that will occur because a file was not selected to be uploaded.

For later use in the script, I elected to define some variables as the next part of the code. The variables are for file size, height and width. The variables are not required (where you could specify the value instead), however they are helpful if you would like to make adjustments, and only in one place.


Accessing the File


Getting information about the file uploaded is next. We will access the appropriate function, and store that data into a variable. The information we will request is file size. This is obtained from the getimagesize() function. PHP assigns a temporary name to the file which is how we access the file within the $_FILES array. The exact code we will use is shown below:





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


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

76,540 Views
8 Total Comments
5 Rating of 5 (1 Votes)

Options

Tutorial Options

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

Articles

Related    

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



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