PHP File Download
Using PHP to Download Files
To download images, zip files, pdf documents, exe files, and other types of files, you usually don't need to use a server-side scripting language like PHP. If such a file is stored in a publicly accessible folder, you can simply create a hyperlink pointing to it, and the browser will automatically download the file whenever a user clicks on the link.
<a href="downloads/test.zip">Download Zip file</a>
<a href="downloads/masters.pdf">Download PDF file</a>
<a href="downloads/sample.jpg">Download Image fil</a>
<a href="downloads/setup.exe">Download EXE file</a>
When you click a link to a PDF or Image file, it will not automatically save to your hard drive. The file can only be opened in your browser.
Using PHP to Force a Download
Using the PHP readfile()
feature, you can force images or other types of files to copy directly to the user's hard drive. We'll make a simple picture gallery here that lets users import image files from the browser with a single mouse click.
Create a file called "image-gallery.php" and paste the code below into it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Image Gallery</title>
<style type="text/css">
.img-box{
display: inline-block;
text-align: center;
margin: 0 15px;
}
</style>
</head>
<body>
<?php
// Array containing
// sample image file names
$images = array("kites.jpg", "balloons.jpg");
// Loop through
// array to create
// image gallery
foreach($images as $image){
echo '<div class="img-box">';
echo '<img src="images/' . $image . '" width="200" alt="' . pathinfo($image, PATHINFO_FILENAME) .'">';
echo '<p><a href="download.php?file=' . urlencode($image) . '">Download</a></p>';
echo '</div>';
}
?>
</body>
</html>
If you look closely at the code above, you'll notice that the download connection points to a "download.php" file, and that the URL also includes the image file name as a query string. Furthermore, we used the PHP urlencode()
function to encrypt the image file names so that they can be safely passed as URL parameters, as file names can contain URL unsafe characters.
The complete code for the "download.php" file, which forces image download, is shown below.
<?php
if(isset($_REQUEST["file"])){
// Get parameters
$file = urldecode($_REQUEST["file"]); // Decode URL-encoded string
/* Test whether the file name contains illegal characters
such as "../"
using the regular
expression */
if(preg_match('/^[^.][-a-z0-9_.]+[a-z]$/i', $file)){
$filepath = "images/" . $file;
// Process download
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system
// output buffer
readfile($filepath);
die();
}
else {
http_response_code(404);
die();
}
}
else {
die("Invalid file name!");
}
}
?>