How to Download File From Folder Using PHP?

1 comment

In this tutorial you are going to learn how to download files from folder in php in just  some simple steps .

I have file  download.php. When user click word “download” the pop up box will appear and save the file.

  Place this kind of link into the document where the file download is offered:
 <a href=”download.php?download_file=some_file.pdf”>Download here</a>

In download.php


//$_GET['download_file']="_11 sv.pdf";
$fullPath = $_GET['id'];

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
fclose ($fd);


Live Demo
Download Demo

Categories: Php, Technologies

One Comment

Leave A Comment