PHP file upload Script :: online picture upload

File Upload 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 Unix 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 adjustments.

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 action='uploadck.php' method=post  enctype='multipart/form-data' >
Upload this file: <input type=file name='file_up'>
<input type=submit value='Upload Image'></FORM>
This html code will display a text box with file upload button. The form tag is bit different than the normal form tag used ( see the encrypte =).

In our php script we will get all details in a global variable like this.
So to get the file size we have to use
Note that in the form we have used the input filed name as file_up. Same way all other information can be collected from the global variable.

Maximum allowed file upload size.

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.
echo "Maximum allowed file size: ".ini_get('upload_max_filesize');
echo "<br>";
echo "Maximum input time : ".ini_get('max_input_time');
echo "<br>";
echo "Maximum execution time : ".ini_get('max_execution_time');
echo "<br>";
echo "Maximum Post size : ".ini_get('post_max_size');
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>";
Here is the full code to handle file upload.
$file_upload_flag="true"; // Flag to check conditions

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

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

// the path with the file name where the file will be stored

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";
echo "Failed to upload file Contact Site admin to fix the problem";
$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.
  • ini_text.php: Check your php.ini file settings.

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net



    how di move the uploaded images from the server to my file in c:drive


    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.


    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


    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.


    thnk the discription above is very good nd easily understandble i learn very well thanks again
    Jayrajsinh Radadiya


    this is very useful for the upload image
    mohamed ashaf


    thks the script above is very useful for me.


    how to i make it custom extension, for example the file can only be an mu1 file?


    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.


    thnk the discription above is very good nd easily understandble i learn very well thanks again


    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


    how to use this coding i am so confussed.
    prince kumar


    how to upload multiple images and store in the folder


    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


    this is very useful code....please tell me how to display stored images in HTML table format


    how to use this coding very confusing.......and where i cn put this folder thumnail


    It will help for us only short commendline we want more information about uploading files are images thank you...........!!!!!!!!!!!!


    Yes i totally understand the stuff here you give...and Thanks for this.


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


    Replace <? with <?Php in both the fiels, or download a fresh copy of zip file.


    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?


    This may happen because of browser showing the image from cache. Try to open in another device and see if the image is changed.


    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?


    Amazing code. Thankyou so much


    I want save my photo in my pc in specific folder,i don't no to how to set a path...
    Govind Verma


    this is easy to learn and implement in program


    nice code for upload image
    A nkit


    How to make an webpage which upload images and then give the link of image where it uploads


    Check how the profile photo is displayed. Link is there at the end of the article.


    To take directly a photo by using computer camera is possible by using additional plugins , not sure how that works.

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer