Creating a file upload form with PHP

This tutorial will guide you for creating a file upload form with PHP.You may need file upload facility on your website for uploading avatars etc.In my example i have coded the script only for images you can customize this script according to your need.

So here is the step by step guide for  creating a file upload form with PHP

1.Create an Upload form with HTML-

upload.php is the file which will  handle the uploaded file after submission.You can save the above html code with your desired name.In next step we will create a php script name upload.php to process and save the uploaded file on the server.

2.Creating a PHP script to handle upload-

This script will perform the following action-

  1. Check  file type
  2. Check file size
  3. Save the uploaded file at a temporary place.
  4. Move the file to upload folder

These  are the steps to perform above operation and complete the upload-

1.Define your settings-Define the variable $allowed_type by filling this variable with allowed extensions  of  uploaded file.Set value for  $max_size in bytes for maximum upload size.Also create and define the upload path to the variable $path.In my example files will be uploaded to upload folder.

 

 2.Get uploaded  file’s detail and check –

 

By default PHP will not handle the file size greater than 2 MB .To increase this limit you may need to change the upload_max_filesize and post_max_size from php setting file php.ini .

3.Moved the temporary file to upload folder-This is the last step for creating a file upload form with PHP. Now its time to move the uploaded file to the upload folder after completing all validation and show the success message to the user.

 

Steps for creating a file upload form with PHP is now completed. you can download the complete source from here.

Download Source

  • vovan

    I have no words – your posts is very understandable !

    Thank you !

  • vovan

    can you show example with sql insert statement ?
    Many thanks.

    • Do not store any file in database .It will consume lots of mysql resources. Store the files on your server and insert the address in your database .

      • vovan

        i going to storing only filename in db, file in /upload dir.
        i just dont know where to put the sql statements in this code.

        • See the third point : put under if condition.

          • vovan

            can you show example of mysql insert image ?

          • if(move_uploaded_file($_FILES[‘file’][‘tmp_name’],$path . $filename))
            {
            $loc=$path.$filename;
            $q=’insert into images (loc) values($loc)’;
            $r=mysql_query($q);
            if(!$r)echo mysql_error();
            echo ‘Your is uploaded successfully

            here‘; // show the link of file to the user }
            else
            echo ‘Unexpected error is occurred while uploading’; //It an unexpected error occurred show this message
            //Creating a file upload form with PHP is now complete.

            ?>- See more at: http://megarush.net/creating-a-file-upload-form-with-php/#comment-233

          • vovan

            thank you ! it really understandable…. can you show with this example how to resize image after upload ?

            regards !

          • After uploading image resize images using this function –http://php.net/manual/en/imagick.resizeimage.php

  • vovan

    Not for this topic but
    Pagination tutorial will be great too !