function districtexcelpdf(){
ob_start();
require_once('districtexcel.php');
$objPHPExcel = new PHPExceldis();
$objPHPExcel->districtExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="District_report.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_clean();
$objWriter->save('php://output');
exit();
}
In above code I got a unreadable character . Required help to solve this problem.
ob_clean(); at the start because it sending a junk value then after use ob_start(); and at the end ob_clean();
Related
having difficulty trying to change the code below for PHP 8.0 which has the above error message:
public function sendNoCacheHeaders()
{
header_remove('Last-Modified');
header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
}
}
I am using the following codes in my controller to download pdf files which are stored in a folder inside application folder in codeigniter.
In the view page I have a button, upon clicking it, it redirects to this controller and then it download the file. It downloads blank page(pdf).
Looking forward to any suggestion.
$filePath = APPPATH.'invoice/'."Let's Sunday#".$this->uri->segment(4).".pdf";
if(!empty($filePath) && file_exists($filePath)){
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" .$filePath);
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($filePath));
flush();
$fp = fopen($filePath, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush();
}
fclose($fp); }else{echo "file does not exist";}
Change the name in Content-Disposition to filename only instead of full path
Here is what I have in a download section within a CodeIgniter app:
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
flush();
exit;
}
The differences are in the ordering of the headers (which likely doesn't matter), the inclusion of the expires and pragma headers, and the Content-Disposition using the full basename.
The naming convention on that PDF filename might cause an issue...something else to test out.
I use Laravel 5.1.
A jQuery ajax call is made like that:
$('#export_selected').click(function(){
var checked = $('.select_rows:checked');
var ids = [];
$.each(checked, function(index, value){
ids.push(value.id);
})
$.ajax({
method : "POST",
url : "{{URL::to('/spot/exportTable')}}",
data : {ids:ids}
});
});
And then the php method is defined that way:
public function exportTable(Request $req) {
$spots = array_flatten($req->all());
$res = Spot::whereIn('id', $spots)->get();
Excel::create('Spots', function($excel) use($res) {
$excel->setTitle('Title goes here');
$excel->setCreator('Creator Goes Here')->setCompany('Company Goes Here');
$excel->sheet('Excel sheet', function($sheet) use($res) {
$sheet->setOrientation('landscape');
$sheet->fromArray($res);
});
})->store('csv', storage_path('/exports', true));
$file = base_path() . '/storage/exports/Spots.csv';
$headers = ['Content-Type: application/csv', 'Content Description: File Transfer', 'Cache-Control: must-revalidate, post-check=0, pre-check=0'];
return response()->download($file, 'Spots.csv' , $headers);
}
Chrome developer console prints the results as raw lines.
The file is successfully exported and created in the disk.
The path to file is correct.
But the download is never started.
Echoing the response gives:
HTTP/1.0 200 OK
0: Content-Type: application/csv
Cache-Control: public
Content-Disposition: attachment; filename="Spots.csv"
Date: Mon, 26 Oct 2015 16:08:26 GMT
Last-Modified: Mon, 26 Oct 2015 16:08:26 GMT
1: Content-Description: File Transfer
2: Cache-Control: must-revalidate, post-check=0, pre-check=0
I put the answer here for everybody having the same issue.
#manix figured it out: I'm trying to download via Ajax, which needs to be done in another way, not the way I wrote my code
To solve the problem that pressing the browser back button to return, after closing session not will start again without asking for data entry, add the following code.
$this->output->set_header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', false);
$this->output->set_header('Pragma: no-cache');
But when I press the back button of the browser I get the message Confirm Form Resubmission.
sorry my bad English
$this->output->set_header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
$this->output->set_header('Cache-Control: no-cache, no-cache, must-revalidate');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', false);
$this->output->set_header('Pragma: no-cache');
Regards,
Using CodeIgniter-PHP and I have a Link (Download Audio) which makes the call to my controller that has the function of downloading audio in. Wav.
As parameter I send the corresponding filename.
Here is my code on my controller:
public function downloadFile($filename) {
$filepath = base_url().'audiofiles/'.$filename;
$filesize = filesize($filepath);
header('Content-Description: File Transfer');
header('Pragma: public');
header('Expires: 0');
header("Content-Type: application/force-download");
header('Content-Type: application/octet-stream');
header("Content-Type: application/download");
header("Content-Length: ".$filesize);
header("Content-disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($filepath);
}
When I click the link I download the wav audio properly, but when I try to play the audio I get that "The file corrupt" - "It needs a special codec"
What is it I'm doing wrong in my code?
Thanks in advance.