Three Methods to Implement File Download in PHP

This article demonstrates three PHP approaches for offering file downloads: a direct link, a parameter‑based redirect, and streaming the file with headers, comparing their implementation details and security implications.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Three Methods to Implement File Download in PHP

The guide presents three ways to provide file downloads using PHP.

1. Directly add file link

HTML code creates a button containing an anchor tag that points to the file URL.

<button>
    <a href="http://localhost/demo.zip">下载文件</a>
</button>

Clicking the button initiates the download.

2. Pass parameters to locate and redirect to the download link

The button’s link includes a query parameter (e.g., f='down') that is processed by a PHP script.

<button>
    <a href="http://localhost?f='down'">下载文件</a>
</button>

The PHP script retrieves the parameter, builds the file name, checks its existence, and either redirects to the file or returns a 404 response.

<?php
$down = $_GET['f'];               // get file parameter
$filename = $down . '.zip';       // file name
$dir = "demo/";                  // download directory
$down_host = $_SERVER['HTTP_HOST'] . '/';
if (file_exists(__DIR__ . '/' . $dir . $filename)) {
    header('Location: http://' . $down_host . $dir . $filename);
} else {
    header('HTTP/1.1 404 Not Found');
}
?>

3. Use head() and fread() to output the file directly to the browser

This method streams the file with appropriate HTTP headers, hiding the real file path.

<?php
$file_name = "down.zip";
$file_dir = "./down/";
if (!file_exists($file_dir . $file_name)) {
    header('HTTP/1.1 404 NOT FOUND');
} else {
    $file = fopen($file_dir . $file_name, "rb");
    Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: " . filesize($file_dir . $file_name));
    Header("Content-Disposition: attachment; filename=" . $file_name);
    echo fread($file, filesize($file_dir . $file_name));
    fclose($file);
    exit();
}
?>
Summary: The first two methods are simple but expose the real file address, reducing security, whereas the third method better hides the file path by streaming the content with proper headers.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPFile Download
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.