I make a php page to look up the database and make Excel file to download
I make a Excel.php
<?php
...
$filename = "Sample";
$objPHPExcel->getActiveSheet()->setTitle("Tasks-Overview");
header('Content-Encoding: UTF-8');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'".xls');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
$objWriter->save('php://output');
exit;
?>
And this is CallFile.php
<?php
$url = 'https://#url';
$ch = curl_init();
curl_setopt($ch,CURLOPT_ENCODING, "UTF-8" );
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_GET,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: appliaction/json'));
$res = curl_exec($ch);
echo print_r($res,true);
?>
And then I click the button, the excel file will download.
When I put address(Excel.php) in Web address bar, it download Excel file success, but when I call CallFile.php it occurs error like this and didn't download excel file.
...
10-1101000000000 � ��*+������&ffffff�?'ffffff�?(�?)�?�"dXX333333�?333333�?U}}}}}}}} � � � � � � � � � � � � � � � � @� � � � � � � @� � � � � � � >�@d��d�Bgg�����
...
Webpage just print error message like break word.
I don't know why I could get Excel file when I call Excel.php,and couldn't get Excel file on CallFile.php
I doubt problem in call api, so I test just echo on Excel.php, and call from CallFile.php, it prints echo without problem.
If you know about that, please help me
You shouldn't use print_r
if you're trying to output a binary file. print_r
adds extra text around variables to make it more readable for people, but that will also screw with it being an excel file. Use echo
instead. Also consider setting content disposition headers like you have in your first example to try and ensure the browser downloads it instead of trying to display it like text.