I am trying to force the browser to download an mp3 when the user visits this site. The issue I am running into is if i set the Content-Length header the page throws a 502 Bad Gateway error, and if I don't it attempts to download the file, but the file is 0 bytes in size. The site happens to be running wordpress. Any direction would be really appreciated. I am not familiar with wordpress.
$file = '../uploads/2015/01/Thunder.mp3';
header('Content-type: application/mp3');
header("Content-Disposition: attachment; filename=\"$file\"");
header('Content-Length: '.filesize($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
Try Adding:
readfile($file);
after:
header('Pragma: public');
Related
Header("location:index.php"); is not working for me.
$file is path of file.
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));
header("Content-Type: application/force-download");
readfile($file);
header("location:index.php");
exit;
}
Your browser makes a call to the server. The server responds with headers ("I will be sending a file") and sends the content. Browser reads headers and instead of showing the new page it offers you to save the file. All further actions are ignored.
If you want both file download and redirect, you have to send the request for download to another container (new tab, new window, iframe) and redirect the main window elsewhere with javascript.
When I server my manifest to Firefox it caches it. What header can I send with the manifest file (which is a PHP page) so it's not cached forever?
/* Firefox still caches the manifest with these headers */
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
This one also is ignored by Firefox:
Cache-Control: no-cache, private
I have a host for store data and a download host (this host doesn't have database). I want to read a file from download host in store host and give it to user for download but I don't want to use monthly bandwidth transfer of store host when user is downloading file and just use download host bandwidth transfer.
There are two ways that I know:
ftp_get download the file and save it in a local file and then set header for download. I don't want use this way because download file in store host.
// in store host
$local_file = 'app.apk';
$ftp_file = '/uploads/2015/06/1eb6a628c60bb69a6b6092d03e252c29.apk';
// download file and save it in local
ftp_get($conn_id , $local_file, $ftp_file, FTP_BINARY);
$file_name = 'app.apk';
$file_size = filesize($local_file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
readfile($local_file);
I don't know file_get_contents use bandwidth transfer of store host when user is downloading file or not.
// in store host
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
// readfile($local_file);
$c = file_get_contents('ftp://login:pass#download-host.com/uploads/2015/06/app.apk');
echo $c;
I don't want to use bandwidth transfer in store host; Which way can I use? Way 2 or another way?
There's no way to download a contents from the "download host" directly to the client, without providing the client with all the information needed for the download ("download link").
If you need to hide the download information from the client, you need to download the file on the "store host" and then forward it to the client. Hence you are consuming bandwidth data of the "store host". It does not matter what technology, protocol or function you use. And the ftp_get and file_get_contents("ftp://...") use both the same code behind anyway.
Simply said, there's no way to both hide the download information from the client and not use bandwidth data of the "store host".
I currently have several servers, the main ones a web server and an internal storage server. Data is stored in a SQL server database.
Without giving too much access to the user, how would I pull a specific document from the storage server for display to the user from the web server?
For example, I have document c:/memberID/info.pdf on the storage server and I want to display it to the client via PHP without giving them access to the internal storage server.
I hope this makes sense!
Look into readfile as well as headers
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
I trying to force a download by the browser here is my code:
header("Content-Type: application/force-download");
header('Content-type: audio/mp3');
header('Content-Description: File Download');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-length: ' . filesize($file_path);
ob_clean();
flush();
readfile($file_path);
This works perfectly on my local machine, but when I upload it to my live server, the binary file is dumped to the browser and those gibberish characters fill up the browser window. What could be the problem? I am on a shared server so I don't know if my apache configuration needs to be changed or something.
I took #sanmi advice and used Firebug to see the response header and here is what I got:
Here is what I got sanmai:
Server nginx/0.7.67
Date Tue, 06 Sep 2011 15:02:03 GMT
Content-Type text/html; charset=UTF-8
Transfer-Encoding chunked
Connection keep-alive
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma no-cache
Vary Accept-Encoding,User-Agent
Content-Encoding gzip
I can see that the content-type entry has changed to text/html, and that the server is actually a nginx one. So is this a nginx issue or are my header entries wrong?
Thanks,
Tsega
It turns our one of the files I include had a blank line after the closing php tag. That was the problem, thanks everyone for your help.
Here
header("Content-Type: application/force-download");
header('Content-type: audio/mp3');
You send two Content-Type, only one is necessary.
I am not sure if this might be causing it, but content type audio/mp3 isn't defined(officially): http://www.iana.org/assignments/media-types/audio/index.html . Try using audio/mpeg?
Use FireBug or other means to view HTTP headers your server actually sending. It may hide or alter any of them. If so, talk to your hosting's support.