How to upload a image using PHP
Posted: Sun Nov 29, 2009 3:21 am
File upload is very common requirement of many web sites. We may require to upload pictures online to websites. Image upload is very common requirement and many sites are using this to allow members or visitors to upload files. Picture rating, picture gallery site uses this feature to allow multiple file uploads. What we need to allow file upload or online picture upload to a site? This file upload script explained in a step by step way to accomplish this online uploading to a web server. We can even create thumbnail images of the uploaded image file by php. The main requirement is to allow the members to browse the local computer and point to the file required for uploading.
Let us add the html code to show the browse button for the visitors to point to file required for uploading. Here is the code.
This html code will display a text area with file upload button. The form tag is bit different than the normal form tag used ( see the encrypte =).
Now let us go to the php part to handle the uploaded file. We have In PHP 3, the following variables will be defined within the destination script upon a successful upload, assuming that register_globals is turned on in php.ini. If track_vars is turned on, they will also be available in PHP within the global
array $HTTP_POST_VARS. Note that the following variable names assume the use of the file upload name 'userfile', as used in the example above ( inside the form):
Let us check the file size and we will not allow file size more than 250 KB to get uploaded to our server. Here we are using a flag $file_upload to false for processing the file upload.
Now let us check the file extension and only jpg or gif file pictures we will allow into our server. We will check this by using $userfile_type extension
We will limit ourselves to these two type checks and if we find our $file_upload variable is not "false" then we can upload the file. This part is very simple in PHP and can be done in one step. Before that let us decide the file directory name where we will be placing the file. $add is the path with the file name relative to the script running this code where the uploaded file will be stored.
Now let us copy the file to server. The command move_uploaded_file will does that job for us and if the action is successful then it will return true.
- Check the file if it is allowed for upload or not. We can check for file size, file extension, file name etc.
- Copy the file to server.
- Place the file in required directory and then give necessary file permission.
Let us add the html code to show the browse button for the visitors to point to file required for uploading. Here is the code.
Code: Select all
<form enctype="multipart/form-data" action="_URL_" method=post>
Upload this file: <input name="userfile" type="file">
<input type="submit" value="Send File"></form>
Now let us go to the php part to handle the uploaded file. We have In PHP 3, the following variables will be defined within the destination script upon a successful upload, assuming that register_globals is turned on in php.ini. If track_vars is turned on, they will also be available in PHP within the global
array $HTTP_POST_VARS. Note that the following variable names assume the use of the file upload name 'userfile', as used in the example above ( inside the form):
- $userfile - The temporary filename in which the uploaded file was stored on the server machine.
- $userfile_name - The original name or path of the file on the sender's system.
- $userfile_size - The size of the uploaded file in bytes.
- $userfile_type - The mime type of the file if the browser provided this information. An example would be "image/gif".
Let us check the file size and we will not allow file size more than 250 KB to get uploaded to our server. Here we are using a flag $file_upload to false for processing the file upload.
Code: Select all
if ($userfile_size >250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$file_upload="false";}
Code: Select all
if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";}
Code: Select all
$add="upload/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name.
Code: Select all
if(move_uploaded_file ($userfile, $add)){
// do your coding here to give a thanks message or any other thing.
}else{echo "Failed to upload file Contact Site admin to fix the problem";}