Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

PHP opens a page with weird characters in production instead of downloading excel file. The same code works fine on localhost

enter image description here This code is supposed to download an excel file, but instead it opens a page in the browser with strange characters.

This only happens in production, while uploaded on the server on siteground.com, but on localhost it works perfectly.

I use phpSpreadsheed, but it does the same thing if I use plain PHP.

Do you have any ideas on how to make it work on production?

<?php
include '../../vendor/autoload.php';

use PhpOfficePhpSpreadsheetHelperSample;
use PhpOfficePhpSpreadsheetIOFactory;
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;

$sql = "SELECT * FROM reserve_tickets";
$result = $connection->query($sql);
$write_array = array();
$fileName = "excel.xlsx";
$write_array[] = array("id","name","address");

if($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc()) 
    {
        $write_array[] = array($row["company_name"],$row["matchmacking_options"],$row["participant_name"]);
    }
}
$connection->close();
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->fromArray($write_array,NULL,'A1');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$fileName.'"');
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');

ob_end_clean();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit();

?>
question from:https://stackoverflow.com/questions/65901385/php-opens-a-page-with-weird-characters-in-production-instead-of-downloading-exce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...