I am working with a friend to help develop a simple interface for a backup system he is operating. Basically, the script displays a secure list of files by filename and each filename is clickable to download the corresponding file. I have forced the download of files using PHP in the past but the method I used required knowledge of the files type. With this project that method is not feasible as this system will have a wide array of possible file types.
I compiled some resources I found online to create the following simple script that forces the download of any file type with PHP: (Note I have not actually tested it with every possible file type but the ones I did used worked great)
<?PHP //BUILD THE FILE INFORMATION $file = $filepath . $filename; //CREATE/OUTPUT THE HEADER header("Content-type: application/force-download"); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-disposition: attachment; filename=\"".basename($file)."\""); readfile($file); ?> |
Perfect for what I was looking for…
Thanks!
At first it seems to work but unfortunately every file I download is 0 bytes. On the server there is text in the file, but when I download it it is blank.
thanks good job….
Hi,
I don’t know anything about php code…
I have the same problem that has been described in this page.
Would you please help me about how can use this code in my html page? ( for downloading mp3 files)
Regards,
Sharareh
thank’s dude that really works. great job man.
How to use this script if I need to call the php file to open a file like an mp3 stored in a folder ‘files/music’ on my webserver?
I copied the code above to a file named download-mp3.php and am using an html link to link to the mp3 file in question
Hi,
I like your code…but i could not understand how it works..
can you send the whole script
thank you
Great article
Hi man! Can you send send me whole script? I think, i found what i looked for…
Hi man! Can you send me whole script? I think, i found what i looked for…
It’s really great!
Exactly what i am looking for..
Thanks!!!!!!:-)
Doesn’t work, placed in a download.php for MP3s…
It just forwards the browser to the download…
i cant understand how to implement this script
let me know how to implement
Excellent script. I would however like to force the file to automatically be downloaded to “downloads” versus giving the user the option to open/save/cancel. Any suggestions.
Thanks,
Grant
great! It is working, thank you for helping me
Nice code…
Thank you. It’s really great!
Exactly what i am looking for.. thanks again