PHP csv下载与服务器上的文件副本不同



我试图创建一个PHP页面,查询数据库,在tmp文件夹中创建一个CSV,然后将该CSV发送到浏览器下载,但下载的文件只包含PHP脚本中的最后一个echo,而不是存储在服务器上的文件(该文件保存在服务器上是完美的(。

<?php

$db_host = "localhost"; //can be "localhost" for local development
$db_username = "root";
$db_password = "";
$db_name = "seinventory";
$link = mysqli_connect($db_host,$db_username,$db_password,$db_name) or die(mysqli_error($link));

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$softwareName = $_GET['soft'];
$result = mysqli_query($link,"SELECT * FROM `seinventory` WHERE software LIKE '%$softwareName%' or apps LIKE '%$softwareName%'");
$timeStamp = date('d.m.Y-h.i.s');
$csvFile = 'C:/xampp/htdocs/tmp/file.csv';
$new_csv = fopen($csvFile, 'w');
$headings = Array('PC Name','Software Name','Software Version');
fputcsv($new_csv, $headings);
while($row = mysqli_fetch_array($result))
{
$pcName = $row['pcName'];
$software = $row['software'];
$app = $row['apps'];
$softwareArray = explode(";", $software);
$appArray = explode(";", $app);
$multiArray = array_merge($softwareArray, $appArray);
foreach ( $multiArray as $value ) {
$singleSoftwareArray = explode(":", $value);
$softwareItem = $singleSoftwareArray[0];
$pcName = str_replace('.domain.local', '', $pcName);
if (stripos($softwareItem, $softwareName)  !== false) {
$singleArray = Array($pcName, $singleSoftwareArray[0], $singleSoftwareArray[1]);
fputcsv($new_csv, $singleArray);
}
}
}
fclose($new_csv);
mysqli_close($link);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="file.csv";');

//unlink($csvFile);
echo "<script>window.close();</script>";

我读了一本应该放出口的书;在fclose之后,它停止向文件写入,但我在服务器上的文件是完美的,不知怎么的,它在下载过程中被更改了。

您必须回显CSV文件的内容才能获得正确的文件。从代码中删除最后一个回显,并将其替换为此回显。

echo file_get_contents('C:/xampp/htdocs/tmp/file.csv');

当你在本地存储文件时,你也可以将用户重定向到一个文件URL,它应该会触发下载。如果您这样做,就不必传递内容处置标头。如果您决定这样做,则必须删除提供内容类型、内容处置标头和最后一个echo语句的行。

header("Location: tmp/file.csv");

如果您只是临时创建文件,然后删除它,那么我建议您应该将数据存储在内存中,然后再回显它。

<?php

$db_host = "localhost"; //can be "localhost" for local development
$db_username = "root";
$db_password = "";
$db_name = "seinventory";
$link = mysqli_connect($db_host,$db_username,$db_password,$db_name) or die(mysqli_error($link));

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$softwareName = $_GET['soft'];
$result = mysqli_query($link,"SELECT * FROM `seinventory` WHERE software LIKE '%$softwareName%' or apps LIKE '%$softwareName%'");
$timeStamp = date('d.m.Y-h.i.s');
$new_csv = fopen('php://memory', 'w+');
$headings = Array('PC Name','Software Name','Software Version');
fputcsv($new_csv, $headings);
while($row = mysqli_fetch_array($result))
{
$pcName = $row['pcName'];
$software = $row['software'];
$app = $row['apps'];
$softwareArray = explode(";", $software);
$appArray = explode(";", $app);
$multiArray = array_merge($softwareArray, $appArray);
foreach ( $multiArray as $value ) {
$singleSoftwareArray = explode(":", $value);
$softwareItem = $singleSoftwareArray[0];
$pcName = str_replace('.domain.local', '', $pcName);
if (stripos($softwareItem, $softwareName)  !== false) {
$singleArray = Array($pcName, $singleSoftwareArray[0], $singleSoftwareArray[1]);
fputcsv($new_csv, $singleArray);
}
}
}
mysqli_close($link);
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="file.csv";');
// set the file pointer position back to 0
rewind($new_csv);
// echo all the contents from current file pointer position(In this case from start of the file)
echo stream_get_contents($new_csv);

最新更新