File upload is very common requirement of many web sites. Image upload is very common requirement and many sites are using this to allow members or visitors to upload files. 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.
In PHP installation ( php.ini file )there is a file size limit for uploading files. You can check your php info to know about this upper limit. You can also run this code to read other parameters affecting your file upload.
From this you can add a check in the script and display message to the user saying about the error. Here is the code for that.
if ($_FILES[file_up][size]>250000){
$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";
}
Here is the full code to handle file upload.
<?Php
$file_upload_flag="true"; // Flag to check conditions
$file_up_size=$_FILES['file_up'][size];
echo "File Name:".$_FILES[file_up][name];
echo "<br>File Size:".$_FILES[file_up][size];
echo "<br>File Type:".$_FILES[file_up][type];
echo "<br>File tmp_name:".$_FILES[file_up][tmp_name];
echo "<br>File error:".$_FILES[file_up][error];
echo "<br>---End of uploaded file details---<br><br>";
if ($_FILES[file_up][size]>250000){
$msg=$msg."Your uploaded file size is more than 250KB ";
$msg.=" so please reduce the file size and then upload.<BR>";
$file_upload_flag="false";
}
// allow only jpeg or gif files, remove this if not required //
if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/gif"))
{$msg=$msg."Your uploaded file must be of JPG or GIF. ";
$msg.="Other file types are not allowed<BR>";
$file_upload_flag="false";}
$file_name=$_FILES[file_up][name];
// the path with the file name where the file will be stored
$add="upload/$file_name";
if($file_upload_flag=="true"){ // checking the Flag value
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
// do your coding here to give a thanks message or any other thing.
$msg="File successfully uploaded";
}else{
echo "Failed to upload file Contact Site admin to fix the problem";
}
}else{
$msg .= " Failed to upload file ";
}
echo " $msg <br><br> <a href=upload.php>Return to File upload </a>";
?>
Uploading file by using JQuery Ajax
Without refreshing pages we can upload files along with other input data by using multipart/form-data. The details we enter along with the uploaded file name can be stored in a MySQL database table.
How to Install and test
Download the zip file at the end of this page.
upload.php : HTML page showing upload button.
uploadck.php : PHP script to check & manage upload.
how di move the uploaded images from the server to my file in c:drive
smo
03-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.
Robert
30-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
lana
29-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.
sandeep
08-02-2010
thnk the discription above is very good nd easily understandble i learn very well thanks again
Jayrajsinh Radadiya
20-07-2010
this is very useful for the upload image
mohamed ashaf
23-02-2011
thks the script above is very useful for me.
none
07-10-2011
how to i make it custom extension, for example the file can only be an mu1 file?
yogesh
25-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.
ramesh
21-06-2012
thnk the discription above is very good nd easily understandble i learn very well thanks again
jarold
30-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
Jothi
20-06-2014
how to use this coding i am so confussed.
prince kumar
07-09-2014
how to upload multiple images and store in the folder
smo1234
07-09-2014
There is a link at the end to next tutorial on multiple file upload. You can upload and give them a unique file name to store in a directory.
pravin dabhi
17-09-2014
this is very useful code....please tell me how to display stored images in HTML table format
parul
29-01-2015
how to use this coding very confusing.......and where i cn put this folder thumnail
aruna
18-02-2015
It will help for us only short commendline we want more information about uploading files are images thank you...........!!!!!!!!!!!!
Aamir
10-03-2015
Yes i totally understand the stuff here you give...and Thanks for this.
devi
25-03-2015
This is the output im getting after using the code above.. could you help me to correct it.. i created 2 php pages one the home page and 2nd the uploadck and the php code was pasted in uploadck .php and when it was previewed in firefox, the below thing was shown. please help me.
250000) { $msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.
"; $file_upload="false"; } if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/gif")) {$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed
"; $file_upload="false";} $file_name=$_FILES[file_up][name]; $add="upload/$file_name"; if($file_upload=="true"){ if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){ }else{echo "Failed to upload file Contact Site admin to fix the problem";} }else{ echo $msg; } ?>
smo
25-03-2015
Replace <? with <?Php in both the fiels, or download a fresh copy of zip file.
Lisa
03-05-2015
I sometimes need a new banner image and we save it using the same file name: on_air_banner.jpg, we upload it wanting it to override the previous file but it won't...what do I do?
smo1234
03-05-2015
This may happen because of browser showing the image from cache. Try to open in another device and see if the image is changed.
mike
18-05-2015
Is there a way to allow the ability to take a pic with computer camera and or phone camera and use that as profile without going through the upload process?
Yogesh
15-06-2015
Amazing code. Thankyou so much
HAMEED FAIZEL
10-03-2016
I want save my photo in my pc in specific folder,i don't no to how to set a path...
Govind Verma
16-03-2016
this is easy to learn and implement in program
Dharam
05-08-2016
nice code for upload image
A nkit
23-08-2016
How to make an webpage which upload images and then give the link of image where it uploads
smo1234
03-09-2016
Check how the profile photo is displayed. Link is there at the end of the article.
smo1234
03-09-2016
To take directly a photo by using computer camera is possible by using additional plugins , not sure how that works.
✖
We use cookies to improve your browsing experience. . Learn more