Friday, May 13, 2016

PHP Download Scripts Two Free PHP Scripts Which Enable Users to Download Files from your Website

Today I am going to share two PHP Script which will be useful for all the Webmasters out there.This PHP Script will enable your visitors to Download various files (be it mp3,movies,zipped etc.) from your website.In short, this is a Download Script and its FREE!

1. Download Script 1

This Download Script works perfectly even when you need to download files from a Protected Directory.Besides this download script also uses Cache control which gives you an option to Open Files for Viewing Without Saving it on your Hard Drive.For example, in case of a text file, you can open the file to view its contents without saving the file on your Hard Disk.The Script is located below:
<?php

// place this code inside a php file and call it "download.php"
$path = $_SERVER[DOCUMENT_ROOT]."/path2file/"; // change the path to your websites document structure
$fullPath = $path.$_GET[download_file];

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 different extensions like doc excel and more
header("Content-Disposition: attachment; filename="".$path_parts["basename"]."""); // use attachment to force a download
break;
default;
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);
exit;
// example: 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>
?>

It must be noted that in the above example ,emphasis is made on pdf files.But you can always add more headers to suit your needs.

2. Download Script 2

This PHP Download script will not show any Confirmation message when the download will complete.
<?php
session_start();
/*
Note that this PHP Script does not show Confirmation message when the Download Completes.You can make the
required changes to suit your needs
*/
//You Can Change the URLs to suit your needs
$error=http://errorexample.com;
$filepath=/my/local/file/path/to/images/;
$getfile = basename ($getfile);

if (isset($_POST[downl_file]) && basename($_POST[downl_file]) == $_POST[downl_file]) {
$getfile = $_POST[downl_file];
$_SESSION[downloadfile] = $_POST[downl_file];

} else {
header("Location: $error");
exit;
}
if ($getfile) {
$path = $filepath . $getfile;
// check that it exists and is readable
if (file_exists($path) && is_readable($path)) {
// fetch the file size and send the respective headers

$size = filesize($path);
header(Content-Type: application/octet-stream);
header(Content-Length: . $size);
header(Content-Disposition: attachment; filename= . $getfile);
header(Content-Transfer-Encoding: binary);
// opens in read only mode
// Ignore error message
$file = @ fopen($path, r);
if ($file) {
// stream the file and exit the script once download is completed
fpassthru($file);

exit;

} else {
header("Location: $error");

}
} else {
header("Location: $error");
}
}
?>
Note that these two PHP Scripts are WORKING!, so you can begin using it straightaway.If you have any suggestions regarding the script please feel free to share it with us.Feel free to edit the above two scripts to suit your personal needs.

Subscribe to our blog and we will email you similar interesting scripts as soon as it is published.

Enjoy!