PHP Upload Files To Folder, Read Files From Folder And Download Files From A Folder.

Learn to upload and download any file(s) using PHP, the easy way.

Last Update: May 14, 2021

PHP Upload Files To Folder, Read Files From Folder And Download Files From A Folder.

Hello friend, today I am going to show you how to upload and download any file(s) using PHP. If you have been struggling to use PHP to upload or download a file, then worry no more.

Note that for the front-end, I am not going to use some fancy styling decorating the structure, the styling used will be just for the practical purpose. First create a folder and call it "FileDownloadUpload" and inside that folder create a "index.php" file and copy the code below and paste it into "index.php" you have just created. Again inside "FileDownloadUpload" folder create another folder called "uploads" that will be used to upload files to it and read files from it.

Note that in the index file, we will create two containers, one will be used to upload files and one will be used to read uploaded files. Now lets get to it.

index.php

<?php
/*
    Author: Mr Parallel
    Tutorial: File upload and download
    Contact: stetcha@panthera-clan.co.za
 */

?>
<style>
    
    body{
        padding-top: 100px;
    }
    
    #upload-form{
        width: 49%;
        overflow: hidden;
        border: 2px solid #0d1219;
        float: left;
        text-align: center;
    }

    form{
        overflow: hidden;
    }
    form h4{
        margin-bottom: 0px;
    }
    form input{
        height: 30px;
        border-radius: 25px;
        outline: 0px;
        margin-bottom: 5px;
        border: 1px solid #414141;
    }
    form button{
        height: 35px;
        padding: 5px 25px;
        margin-top: 15px;
        background: #0d1219;
        color: #fefefe;
        border: 1px solid #414141;
        border-radius: 25px;
        outline: 0px;
    }
    #uploaded-files{
        width: 50%;
        overflow: hidden;
        float: right;
        border: 2px solid #007052;
        text-align: center;
    }
    #uploaded-files li{
        list-style: none;
        margin-bottom: 10px;
    }
</style>

<?php
    if(isset($_POST["upload_file"])){ //NOTE THAT WE USE "POST" METHOD AS WE STATED ON THE FORM
                
        if(isset($_FILES["file_to_upload"]) and $_FILES["file_to_upload"]["size"]>0){
            
            $uploaded_file = $_FILES["file_to_upload"]["tmp_name"];
            $uploaded_file_name = $_FILES["file_to_upload"]["name"];
            
            if(move_uploaded_file($uploaded_file, "uploads/$uploaded_file_name")){
                echo "<h2>The file has successfully been uploaded</h2>";
            }
            else{
                echo "<h2>Error occured</h2>";
            }
        }
        
    }
?>

<html lang="en">

    <head>
        <title>File upload and download</title>
    </head>

    <body>
        <div id="upload-form">
            <form method="post" enctype="multipart/form-data">
                <h1>Upload File</h1>
                <h4>File To Upload *</h4>
                <input type="file" name="file_to_upload" required />
                <br>
                <button type="submit" name="upload_file">Upload File</button>
            </form>
        </div>    
        <div id="uploaded-files">
            
            <h1>Uploaded Files</h1>
            <?php
                $array = glob("uploads/*"); //The function "glob()" is used to capture files at specified directory as an array
                $item_numbers = count($array); //We will print number of files in the uploads directory
                echo "<h3>Uploaded Files => $item_numbers files</h3>";
                foreach($array as $file){ //Now we will print those file residing at uploads directory
                    echo "
                        <li>
                            <a href='download.php?file=".basename($file)."'>".basename($file)."</a>
                        </li>
                    ";
                }
            ?>
        </div>    
    </body>

</html>


The above code will yield this:


As you can see from the above image, on the left hand side, we have a form used to upload files and on the right hand side, we have a container reading the number of files uploaded to the "uploads" folder. Now lets upload a file and see.



We have just uploaded a file, and it was uploaded successfully, and you can see on the right hand side that we have 1 file on "uploads" folder.

Well that is awesome, now the big question comes, "How to download the file?". That is a good question, programmatically to answer that, now create a new file called "download.php" inside the main folder, what I mean by the main folder, I am talking about "FileDownloadUpload" folder, and paste the below code into "download.php" file.

download.php

<?php
if(isset($_GET["file"])){
   $file = $_GET["file"];
   $target_file = "uploads/$file";
   $content_type = mime_content_type($target_file); //Used to detect what type of file is been called, an image, pdf, or any
   if(file_exists($target_file)){
      header("Content-Type: $content_type");
      header("Content-Length: " . filesize($target_file));
      header('Content-Disposition: attachment; filename="'.basename($target_file).'"');
      readfile($target_file);
   }
   else{
      exit();
   }
}
else{
   exit;
}
?>


And well that's it, you can now upload and download files, using PHP, and well in a simple way. So let's attempt to download any file and see.

I must say, I have just downloaded the file, what can I say!!!

Source Code: Download Source Code


Thank you for visiting the site.


Regards

Mr Parallel