How do I use PHP to create a file download system?

How to Create a File Download System using PHP

In this blog post, we will learn how to create a simple file download system using PHP. We will create a script that will allow users to download files from the server. The process involves reading the file from the server and sending its contents to the user’s browser with appropriate headers.

Step 1: Create a PHP script to handle file downloads

Let’s start by creating a new PHP script called download.php. This script will handle the file download process. It will receive a filename as a GET parameter and then serve the file to the user for download.

Here’s the code for download.php:

<?php
# PHP
if (isset($_GET['file'])) {
    $file = $_GET['file'];

    // Validate the file before processing
    if (file_exists($file) && is_readable($file)) {
        // Set appropriate headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));

        // Read the file and send it to the user
        readfile($file);
    } else {
        echo "Error: File not found or not readable.";
    }
} else {
    echo "Error: No file specified.";
}
?>

Let’s go through the code:

  1. First, we check if the file parameter is set in the GET request.
  2. We validate the file by ensuring it exists and is readable.
  3. We set the appropriate headers to indicate a file download, the file type, and the filename.
  4. We set cache-related headers to ensure the file is not cached by the browser.
  5. We set the Content-Length header, which is the size of the file in bytes.
  6. We use the readfile() function to read the file and send its contents to the user’s browser.

Step 2: Create an HTML file to trigger the download

Now, we need to create an HTML file that will link to our download.php script. When users click on the link, the download process will start.

Here’s the code for index.html:

<!-- html -->
<!DOCTYPE html>
<html>
<head>
    <title>File Download System using PHP</title>
</head>
<body>
    <h1>File Download System using PHP</h1>
    <p>Click the link below to download the file:</p>
    <a href="download.php?file=example.txt">Download example.txt</a>
</body>
</html>

In this HTML file, we have a simple link to our download.php script, passing the file parameter with the desired file name.

Conclusion:

In this blog post, we have learned how to create a basic file download system using PHP. The download process is triggered by a link in an HTML file, and the file is served to the user by a PHP script that reads the file and sends its contents to the user’s browser with appropriate headers. This is a simple yet effective way to create a file download system using PHP.

Got question?

Submit it here

© All rights reserved.