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
Related
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?
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);
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.
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.
I have a php script that I have used for years to force downloads from my website. But sometime in the last month or so, it stopped working and is triggering file not found errors. The weird thing is that in firefox, if I do view source on the error page, it is the file I was trying to download. And doing File > Save from there give you the correct file. So I know it's not a problem with the script not finding the file on the server.
Is there something wrong with how I am setting up the headers?
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-length: '.filesize($file_url));
header('Content-disposition: attachment; filename="'.basename($file_url).'"');
readfile($file_url);
Can you try this function?
function force_download($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, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
I ended up cheating to make this work.
header("Location: $file_url"); //file_url is now the real url, not the path
And then used cPanel to make sure all the MIME types I was using were set to application/octet-stream.