Download mp3 file from android browser - php

I referred to these questions and implemented their answers but none of them is working for me.
download mp3 instead of playing in browser by default?
PHP Streaming MP3
content type for mp3 download response
download mp3 from web url with android failed
Basically when the user clicks a download link on the website, we want the user to be prompted to download the audio file.
Code :
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=".$file_name);
readfile($fname);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
Here, $mtype is "audio/mpeg"
Success :
It's working fine on all desktop browsers.
I tried with a BlackBerry, and I got the downloaded file.
Failure :
when i do same with android browsers, I did't get the downloaded file.
file name also missing, comes with <untitled> and shows the status "download unsuccessful".
Thank you if you have any idea why this code doesn't work on an Android phone.

Having the same problem and it could be that ur server does not allow you to use mp3 files properly ( I use Biz.nf )

I have just found solution of this problem. On my android Xiaomi it works perfectly.
if (file_exists($file)) {
echo "OK";
if (ob_get_level()) {
ob_end_clean();
} // clearing output script buffer
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
header('Content-Description: File Transfer');
header('Content-Type: {$mime_type}');
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));
readfile($file);

Related

corrupted file by saving with CodeIgniter and header output

I want to download a file from the upload folder by calling mydomein/download/filenem
It works in this way, that I received the wanted image, but I think I have a bug in the header which I output into my controller with this code:
$file=my_path_tofile;
header('Expires: 0');
header('Content-Description: File Transfer');
header("Content-type: ".$finfo->file($file));
//header("Content-type: application/".$ext);
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="'.$data['realname'].'"');
header('Cache-Control: must-revalidate');
header("Content-Length: " . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($file);
exit;
If I call the URL, the download will start, the filename is fine and the size looks right too. But if I want to open the file I get the message that the file could not open in cases of corruption.
I'm not an expert about headers, so can someone explain me what is the problem is and how I can solve it?

Sending a .docx file to the Browser with PHP

I am struggling to send a Word .docx file to the browser. The Word file opens fine, if I create a link to the Word file, it also opens fine.
However when using this code:
// output the file to the browser
if ($fileToOutput) {
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
//header('Content-Length: ' . filesize($fileToOutput)); // this is causing the file download to fail
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($fileToOutput) . "\"");
ob_clean();
flush();
readfile($fileToOutput);
exit;
}
I get the following:
Followed by the dreaded "Word experienced an error trying to open the file.":
Like I said, the Word file opens fine, and downlaods from a link fine - I only get these errors when trying to output the Word file with the above PHP code.
Where is this PHP code going wrong?

CSV file doesnot downloaded from server in php

I am new to php and have php code which download record from server.
It works fine on my local server but does not work at server side.
at server side it redirect me to controller but in local in works fine.
To download the csv file i have created a new controller file.
here is my code
through this javascript code I am calling my controller
function exportcustomer()
{
window.location=site_url+'controllers/ajax_controller/user-export-controller.php';
}`
and here are the headers
$filename = "UserReport.csv";
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Pragma: public'); // required
header('Content-Length: '.filesize($filename)); // provide file size
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header("Content-Type: application/download");
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary ");
header("Content-Type: application/octet-stream");
readfile($filename); // push it out
echo $output;
exit;
Do we need to restart server after adding any new controller.
please let me know what is the mistake in this code.
Thanks in Advance

Download Excel From Server Using PHP

Can anyone help me to write code on PHP to download Excel File from the server.
I have created below codes using header and readfile but the downloaded file was corrupted.
//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$fileName);
//read from server and write to buffer
readfile($reportPath);
Can anyone help me on the best way to download existing Excel file from the server.
Please see below image of data after downloaded
ob_clean();
put that code before all header declaration
I suggest to use the X-SENDFILE header. https://tn123.org/mod_xsendfile/
I use something like the following:
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Length: ".filesize($path));
readfile($path);
exit;
Make sure you're not outputting anything before or after the readfile.

PHP Download Script Creates Unreadable ZIP File on Mac

For reference, I have already read and tried the answers in these and several other threads:
Creating and serving zipped files with php
Opening downloaded zip file creates cpgz file?
I have a zip file on my server.
When I use Filezilla to move that Zip file from my server to my Mac, I can open it normally.
When I use this PHP code to download the Zip file to my Linux machine, it opens normally.
When I use this PHP code to download the Zip file to my Mac, using Safari or Firefox, I get an error saying "Decompression Failed" or "The structure of the archive is damaged" or I get a .cpgz file - which I believe means that the computer is zipping the file, not unzipping it.
Here is the PHP code I am using to deliver the zip file.
$zipname = "myfile.zip";
$zippath = "/path/to/" . $zipname;
if ($downloadzip = fopen ($zippath, "r")) {
$fsize = filesize($zippath);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-length: $fsize");
header('Content-Transfer-Encoding: binary');
#header("Cache-control: private"); //use this to open files directly
echo fpassthru($downloadzip); // deliver the zip file
}
fclose ($downloadzip);
I found some headers that work. I don't really know or care why it work, I am just happy it works... I tried a ton of different things, .htaccess files, php.ini / zlib settings.
Here's the answer
http://perishablepress.com/http-headers-file-downloads/
$zipName = 'myfile.zip';
$zipPath = 'mydirectory/' . $zipName;
if (file_exists($zipPath)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$zipName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipPath));
ob_end_flush();
#readfile($zipPath);
}
Often the issue is caused by extra characters that have been printed or echo'd to the page before you read out the file. Even a space will cause the failure. To fix that issue, call ob_end_clean(); before you read the file which will clear the output buffer and turn off buffering.
But keep in mind you can have nested output buffers, and this will corrupt your download as well (cheers to Vladamir for figuring this out). So to clear the output buffer completely run this before you read your file:
while (ob_get_level()) {
ob_end_clean();
}
This will clear out your entire buffer and you won't have any extra characters to mess up your download.
For those interested i've pasted my download script below. My zip files now download perfectly, and so far this works great.
if (file_exists($zip_file_path)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//We can likely use the 'application/zip' type, but the octet-stream 'catch all' works just fine.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='$zip_file_name'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zip_file_path));
while (ob_get_level()) {
ob_end_clean();
}
#readfile($zip_file_path);
exit;
}
Here is what works
$zipName = 'myfile.zip';
$zipPath = 'mydirectory/' . $zipName;
if (file_exists($zipPath)) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$zipName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($zipPath));
ob_end_flush();
#readfile($zipPath);
}
Well, I presume you know that your $fsize variable is not being written to that header because it's enclosed by quotes.
You could try something like this:
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=\"".$zipname."\"');
header('Content-Type: application/zip');

Categories