Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP download file script code example
<?php
echo '
<a href="http://mydomain.com/download.php?download_file=some_file.pdf">PHP download file</a>';
<?php
$file_id = 123; // or something else you got from your MySQL database
$slug = md5($secret.$file_id);
echo '
<a href="http://mydomain.com/dowload.php?fid='.$slug.'">PHP download file via MySQL</a>';
<?php
ignore_user_abort(true);
set_time_limit(0);
$path = "/absolute_path_to_your_files/";
$secret = 'your-secret-string';
if (isset($_GET['fid']) && preg_match('/^([a-f0-9]{32})$/', $_GET['fid'])) {
$db = new mysqli('localhost', 'username', 'password', 'databasename');
$result = $db->query(sprintf("SELECT filename FROM mytable WHERE MD5(CONCAT(ID, '%s')) = '%s'", $secret, $db->real_escape_string($_GET['fid'])));
if ($result_>num_rows == 1) {
$obj = $result->fetch_object();
$fullPath = $path.$obj->filename;
if ($fd = fopen ($fullPath, "r")) {
//
// the other PHP download code
//
}
fclose ($fd);
exit;
} else {
die('no match');
}
} else {
die('missing file ID');
}
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_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");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
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;
@b8engl

This comment has been minimized.

Copy link

@b8engl b8engl commented Feb 28, 2020

Doesn't work

@finalwebsites

This comment has been minimized.

Copy link
Owner Author

@finalwebsites finalwebsites commented Feb 29, 2020

Doesn't work

It's basic code and should work for most of us. Any error's in you log files?

@solihr

This comment has been minimized.

Copy link

@solihr solihr commented Feb 17, 2021

can we use it for Dropbox link?

@finalwebsites

This comment has been minimized.

Copy link
Owner Author

@finalwebsites finalwebsites commented Feb 22, 2021

can we use it for Dropbox link?

Sure, if you use the Dropbox API. This code is only for sending a file to the browser, nothing else ;)

@solihr

This comment has been minimized.

Copy link

@solihr solihr commented Feb 22, 2021

i created download.php and put/replace the Dropbox link at this line -> $path = "/absolute path_to_your_files/"; but returns 404 page

@finalwebsites

This comment has been minimized.

Copy link
Owner Author

@finalwebsites finalwebsites commented Feb 22, 2021

This is not how it works, this snippet is for files you process on your own server.
What you try is not possible with that script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment