Upload File Using PHP and Save in Directory

Demo Download

Uploading file is one of the most common feature which we normally use in our daily life, we do upload pictures on Facebook, Twitter and other websites. So today i will share a tutorial about how to upload file using PHP and save/store that file in your web server directory.

HTML Form

We will need a form with one input field with file type. The action tag of form should point to the URL of PHP script file which actually perform the uploading task. It is also important to add  enctype="multipart/form-data" property in your form to upload file otherwise your file will not be uploaded.

PHP Script

As you can see above i tried to use the variable name as its function so that you can easily understand what i am doing, i just check that if file already exist it will print the error otherwise file will be uploaded.

This tutorial is only for the basic learning purpose, if you want to implement it on your website so it is better to use more validation before uploading file.

Validation such as file type (extension) or file size can also be check on the client side i also wrote tutorial Check File Size & Extension Before Uploading Using jQuery.

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

15 Comments

  1. Do you have coding php for upload and display image file into mysql database? I need guidance for database connection for this method.

    1. Images are uploaded in Host like you upload other files and logo etc. But the location of uploaded image is stored in database, so it could be something like that for example: images/upload/imagename.jpg, it will be store in database and file will be uploaded in your host not in database, hope this will help you.

  2. Thanks for sharing this wonderful tutorials. I was wondering if you could elaborate or expound some more on the variation of the code if you wanted to upload multiple pictures to the same record in the database? Thanks in advance, Chris

    1. Make sure that you are using enctype=”multipart/form-data” also make sure that you are testing this script either on server or if you are using it on local machine then files must be your localhost, such as Apache or any other server.

  3. Hello Friend..
    To some way to make the Upload that is done be saved at home different login account .. And that each person can see what put in the upload.

  4. Hi Javed,

    nice article.

    Would you mind checking your code. The code is not working as expected.
    I don’t see any declaration of ‘my_file’

    1. Hi Absar,

      I have tested code, it is working fine. And you can see that my_file is the file name, as you can see in the HTML section.
      name=”my_file”
      If you are still facing problem, kindly explain more about your issue.

Leave a Reply

Your email address will not be published. Required fields are marked *