SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP file upload Script :: online picture upload

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.
  • 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.
 Before starting we must add file permission to the directory where we plan to store the files after uploading it. Let us give the directory name upload and give write permission to it. If it is a Window server nothing is required and by default window gives all permissions. For Linux and Uinx we have to give write (Chmod) permission to allow uploaded files to store.

If you are uploading large files then read about maximum file execution time allowed and its adjuestments.

 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

<FORM ENCTYPE="multipart/form-data" ACTION="_URL_" METHOD=POST>
Upload this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>


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):
  • $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".


With all these info we will make our script ready to handle the files.
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.

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";}



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

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";}

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.

$add="upload/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name.

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.

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";}

Thats all... the file is placed in our directory (name: upload)
This is the basic file upload script. The process of coding is same but in PHP 5 and above the details information related to uploaded file is to be handled in a different manner, by using $_FILE array. Read how file upload script is to be modified to take care of register_global in PHP 5.



shabeer02-09-2009
how di move the uploaded images from the server to my file in c:drive
smo03-09-2009
After uploading you can display the hyper link by adding the file name. Once the link is clicked then a new window will ask you to save or open the file. That time you browse and show the location of C drive.
Robert30-09-2009
I understand the pointing to the php file in the particular directory...but when it is online it opens a window with just the code in it?? help
lana29-10-2009
i have added to my site and it works excellently except for large files... it seems to take forever... any tips - or is php inefficent at large scale images? 2ndly is there a way to provide the error messages underneath the form itself instead of on a new page? thanks.
sandeep08-02-2010
thnk the discription above is very good nd easily understandble i learn very well thanks again
Jayrajsinh Radadiya20-07-2010
this is very useful for the upload image
mohamed ashaf23-02-2011
thks the script above is very useful for me.
none07-10-2011
how to i make it custom extension, for example the file can only be an mu1 file?
yogesh25-04-2012
1-How to upload images in PHP all the process with code and step wise give answers pls help..

2-How to remove or Modified image in PHP.
ramesh21-06-2012
thnk the discription above is very good nd easily understandble i learn very well thanks again
jarold30-01-2013
thanks it gives me an idea....recently we are trying to make a program to upload a photo but we need to make it as our profile picture...can anyone help me or give code for this
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Join Our Email List
Email:  
For Email Newsletters you can trust
File handling
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2013 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer