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: 1 of 5
(View All)

Introduction


Utilizing the ability to upload files with PHP can be a great asset to your site. You will find that the code to initiate a file upload is quite simple, and most of the script is based on limitations, such as: file type filtering, cross browser recognition and error detection. You want to make sure that images are being uploaded, and not php scripts with an image header. You also want to write your code so that it will execute properly for the various popular web browsers used. It is vital to the script to be provided with all of the required input to complete the request.

Based on the design that I will discuss in the article, the overall construction of this script will be encapsulated in an IF statement (2 parts - true and false), with the condition based on the status of a button. The basic algorithm for the condition will be: If the submit button was pressed, evaluate image to upload. If not (submit not pressed), show the form to upload the image.

Input Form and Elements


Your first step is to build a form to which the user will use to upload the file. In your form code, you will need to specify the form type as "enctype = multipart/form-data". Specifying this form type allows you to send the request to upload the file. If you omit this line, the form will not function properly. Once the submit button has been pressed, we will then validate the file in queue to be uploaded.
The code below shows exactly what you will enter:

The code above specifies the form type to be able to perform the upload. The action of the form will refresh to the same page via the post method, and the name of the form is stated.

Next, you can also use a hidden form element to specify the max file size of the intended file to upload. The size is specified in bytes like this:

Listed below is a quick reference outlining file size:

8 bits = 1 byte
1024 bytes = 1 kilobyte
1024 kilobytes = 1 megabyte
1024 megabytes = 1 gigabyte

For uploading images, you should not be past the megabytes. Many digital cameras take high-resolution images that can be megabytes in file size, but you do not want 100's or 1,000's of users uploading images that are that large. The bandwidth and storage for your site could shrink rather quickly. However, enter in the appropriate value in byes that you would like to use for your site.

Now that you have the form type identified and a max file size specified, next you will need to put in the form element that allows the user to browse their HDD and select the file (image). See code below:

Note: Initially, the user can select any file to upload of any size. It will be after they press the submit button that we begin filtering through the request.

Name entered here will be stored in the $_FILES array, so we will be looking for $_FILES['userfile'] when the submit button has been pressed. Input type = "file" is the code used to create the form element to browse your computer for a file to upload. The size specified is for the size of the form element, not the size of the file.



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


Comments:

  shay
  Subject: "hi" Date: Dec 10 2007 at 9:28 am    

how is everyone
  MrRSMan
  Subject: "Nice one" Date: Mar 22 2008 at 11:55 am    
Impressed- very helpful article!
You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

71,402 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-2014; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank