Home>
Currently download a CSV file by pressing the button in PHP
I'm creating it, but the file is being created but not downloaded.
I'm investigating a lot, but I can't find a solution.
Please give me your help.
<? php
/ **
* CSV output
* /
// include required files
include_once ('hogehuga.php');// Connection information and search functions are included in this.
// execute connection
global $serverName, $conInf;
$conn = sqlsrv_connect ($serverName, $conInf);
$mainData = array ();
$stmt = getHogeList ();
while ($row = sqlsrv_fetch_array ($stmt)) {
$hogeNo = $row ['hoge_no'];
$hogeName = mb_convert_encoding ($row ['hoge_name'], 'UTF-8', 'SJIS-WIN');
$hogeDate = $row ['hoge_date'];
// add row
array_push ($mainData, [$hogeNo, $hogeName, $hogeDate]);
}
sqlsrv_free_stmt ($stmt);
// Close connection
sqlsrv_close ($conn);
// generate file
$fileName = 'HogeFuga.csv';
$csvFile = 'TEMP_' .time () .rand (). '. csv';
$res = fopen ($csvFile, 'w');
foreach ($mainData as $line) {
mb_convert_variables ('SJIS', 'UTF-8', $line);
fputcsv ($res, $line);
}
fclose ($res);
// Start downloading
header ('Content-Type: application/csv');
header ('Content-Disposition: attachment;filename = "'. $fileName. '"');
header ('Content-Length:' .filesize ($csvFile));
readfile ($csvFile);
?>
-
Answer # 1
Related articles
- php - writing to a csv file does not work
- download 10gb file from aws s3 with php
- [php] cannot download zip file
- ruby - i want to load a csv file into mysql but it doesn't work
- cannot get the file name of the url of another page with php
- javascript - how to read a csv file like jsonstringify ()
- output image file with php
- php - i want to save the file in the specified directory
- php - [errno 2] no such file or directory
- php file not working
- python - csv file split
- import mysql csv file
- can't get file list in directory well with php on windows
- ruby - csv file reading in rails
- when i read a csv file into datatable, it gets null
- i want to download the file identified by php in the button format (i want to assign the php variable to the html download link?
- i want to upload a csv file at jupyter labo
- php - download images made with javascript
- jupyter lab csv/xlsx file does not open
- php file send pure white
Trends
It seems that there is no problem in terms of flow
Stop all spitting headers
readfile ($csvFile);
It ’s a good idea to see if you can check the text content with