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.

5 Comments

  1. 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.

  2. 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.

Leave a Reply

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