I have PHP files stored on my server, and their names in the mysql database, I want to download those files. What code should I write for the same? I am using PHP as coding language. Please help.
Asked
Active
Viewed 630 times
-3
-
just select your question title and right click and click search on google, you will get solution on first link. – Ahmed Sunny Aug 28 '17 at 16:00
-
@KevinNelson I was about to use your possible duplicate to close this question, but the OP's story seems to go further: *"and their names in the mysql database"* - TBH, I'm "on the fence" with this one. – Funk Forty Niner Aug 28 '17 at 16:03
-
@Fred-ii-, hmm...yeah...maybe I jumped the gun because it was a question without more details of what areas he's struggling with. I was assuming since he put the names in the DB that he knows how to get the names out, and he just didn't know how to do the output. – Kevin Nelson Aug 28 '17 at 16:06
-
@KevinNelson Maybe not (jumped the gun), given the answers (so far) below. – Funk Forty Niner Aug 28 '17 at 16:06
-
1Other example with DB query: https://stackoverflow.com/questions/36730545/php-how-to-make-browser-to-download-file-on-click – Kevin Nelson Aug 28 '17 at 16:09
-
@KevinNelson I used the duplicate you originally marked it off with as a possible duplicate and the additional link you included above, *thanks*. – Funk Forty Niner Aug 28 '17 at 16:12
-
1@rugwed, I'm not trying to beat on you, but for future reference, this may help you ask questions that don't get voted down: https://stackoverflow.com/help/how-to-ask – Kevin Nelson Aug 28 '17 at 16:12
-
No problem, I will keep it in the mind henceforth. Thanks though. @KevinNelson – rugwed Aug 29 '17 at 05:42
3 Answers
0
<?php
$file = 'send_me.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
Obviously, set $file
to the file name.
Read more about the use of readfile here.

Bitz
- 1,128
- 11
- 33
0
<a href="$result->pathToServer">Download</a>
?
Literally just make a link the stored file.

Soundz
- 1,308
- 10
- 17
0
file_put_contents("PDFName.pdf", fopen("http://someurl/PDFName.pdf", 'r'));
You really should show what you have done so far/researched online before asking a question! This will download the file PDFName.pdf from the url http://someurl/PDFName.pdf and put it into the same directory as the script is in.

Conor Thompson
- 198
- 1
- 15