Download Excel From Server Using PHP - 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.

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?

Download mp3 file from android browser

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);

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

PHP forcing browser to download files (two ways)

I want a php script to download files of any type without opening it. I have found the following functions, which seem to me a bit different, but I don`t know which is better. Please let me know which one is better and reliable:
From PHP Manual
$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;
}
From other tutorial:
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-Disposition: attachment; filename=\"{$file->filename}\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize);
// download
// #readfile($file_path);
$dwn_file = #fopen($file_path,"rb");
if ($dwn_file) {
while(!feof($dwn_file)) {
print(fread($dwn_file, 1024*8));
flush();
if (connection_status()!=0) {
#fclose($dwn_file);
die();
}
}
#fclose($dwn_file);
}
Both are actually pretty similar when you look at the headers that are being sent, which is what forces the download. The difference is in how the file is read. The first one uses readfile() as an abstraction while the second one reads byte per byte.
The second example also uses the # symbol several times to suppress errors which may not be a good thing to copy.
I'd use the code from the PHP manual. It's simpler, less error-prone, and more readable.
They are both basically the same. What you want is the headers
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"{$file->filename}\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize);
header('Content-Type: image/gif');
And then simply output the file. This can be done in numerous ways, but I would recommend simply:
readfile($filename);
as it is self-explanatory. It will read the file and output it to the output buffer (i.e. the browser).
Note however that the Content-Type header should be set to image/gif if that is what you are outputting.
This is an opinion, but I would use the first one, it's a lot quicker and cleaner.

PHP - What are the correct headers for downloading file?

I have a piece of code that allow users download file from server (document such as docs,docx,pdf etc).
Users can download files but it has some errors like the files were broken. For example, a MS Word file after download need to recovery to read content.
I wonder that if there is any mistake in this code (or problem when uploading?).
$size_of_file = filesize($download_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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: ' . $size_of_file);
//read file from physical path
readfile($download_path);
Did you try like this ?
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
I found the root of the problem, I hav some extra spaces after php close tag. Thank you guys.

Categories