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

<form name="form" method="post" action="upload.php" enctype="multipart/form-data" >
<input type="file" name="my_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</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

if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
	$target_dir = "upload/";
	$file = $_FILES['my_file']['name'];
	$path = pathinfo($file);
	$filename = $path['filename'];
	$ext = $path['extension'];
	$temp_name = $_FILES['my_file']['tmp_name'];
	$path_filename_ext = $target_dir.$filename.".".$ext;
 
// Check if file already exists
if (file_exists($path_filename_ext)) {
 echo "Sorry, file already exists.";
 }else{
 move_uploaded_file($temp_name,$path_filename_ext);
 echo "Congratulations! File Uploaded Successfully.";
 }
}

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.

//you can easily get the following information about file:
$_FILES['file_name']['name']
$_FILES['file_name']['tmp_name']
$_FILES['file_name']['size']
$_FILES['file_name']['type']

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.

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

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.
  1. Sir your help is required ,please help me . I have a file.php page on which i put one and

    I want to select a file may be .pdf or .doxc from my local folder and on button click it will be display on the same page below the button click without uploading it in to db. Please help me….

  2. I am trying to use your code but when I click the submit button it just opens a tab with the PHP script. How do I fix this? just a beginner sorry.

  3. Hi, I want to know how to upload multiple videos in a specific category and store in the specific folder category. So each file will be store in specific folder, so it will be easy to manage folders and files.

    1. Hi Sami, you will need to provide a drop-down list at the time of video uploading, based on selected category, you can change the location where you want to upload file, you can check on server side the category id and based on id change the folder name so that file will be uploaded in that specific folder.

  4. Hello Sir Javed your code worked perfectly ! but after saving the file in our folder I need a way of reading it and sending the data back to the client side . I am saving a .iif file which is a quickbook extension . How can I achieve my goal of reading an iif file in php and sending data as an array

  5. I have:
    Warning: Undefined array key “my_file” in C:\xampp\htdocs\Thales\upload.php on line 2

    Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\Thales\upload.php on line 2
    What is wrong ?

  6. Hi,
    Ubuntu 18.04.5
    LAMP – PHP 7.2

    Used your pack changing the upload directory path. Given directory permissions with ‘chmod’. On browsing, the index page showing properly (http://localhost/filesupload) file selection is also working. But when I click the upload button it’s redirecting to a blank page, nothing happening (in browser path: http://localhost/filesupload/upload.php). Is there I am missing anything in the configuration?

    1. If you are using the similar code so there should be no problem. However, if you are still facing error, you will need to debug the code via printing the value of submitted by form.

  7. Dear Javed

    I think the reason why the uploaded file wouldn’t show in the uploaded folder because you haven’t put a php.ini which allows files to be uploaded file_upload=on

    Thanks levi

  8. Indeed, useful tutorial.
    Couple of things to focus if you are getting an error: HTML Form always needs to be “Post” method and You should create “Upload” folder manually, else uploading will not work well.

  9. Hi nice work but i need that when i am uploading, i can select a folder in which to upload. i have different file to upload to different folders

    1. Well common practice is that we never tell the user that in which location we are upload files for security reasons but if you want to build application for your custom limited users, you can add additional field with options of location and based on these location selection, upload the file in the selected location.

    2. Hello guys , why it gives me the message that file was uploaded but I don’t find it on my web site folder where I wanted it to store. When I do on windows it works very so well why it can’t on my website online.

  10. Notice: Undefined index: my_file in C:\xampp\htdocs\test\upload.php on line 13

    Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\test\upload.php on line 13

    getting this error

    1. Warning: move_uploaded_file(upload/BSError.PNG): failed to open stream: No such file or directory in C:\xampp\htdocs\test\upload.php on line 28

      Warning: move_uploaded_file(): Unable to move ‘C:\xampp\tmp\php6FCE.tmp’ to ‘upload/BSError.PNG’ in C:\xampp\htdocs\test\upload.php on line 28

  11. Well done my friend.. i achieved to implement to my existent project. P.S Guys, first just do quick a new project foowing his advices, and then adapt to your… there is no where a full website to copy/paste. Get info, if it works, folow the guy, and put your brain to work. 3 EXAMPLES TOOK FROM HIM UNTIL NOW, AND IT WORKS GREAT IN REAL PROJECTS!

  12. Notice: Undefined index: my_file in D:\xampp\htdocs\upload\upload.php on line 2

    Sir I’m getting this error when I run the code. What to do?
    Please reply soon.

    1. You need to make sure if form is submitted then check file, simply download tutorial from the download button and execute it in your localhost or live host.
      I am pretty sure, you are not checking that form is submitted or not.

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

      1. Could the reason why it not appearing in the folder once uploaded could be the .htacess file won’t allow it

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

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

  16. 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 *