为什么我的 fputcsv 不起作用?
Why won't my fputcsv work?
我无法使 PHP built-in 函数 fputcsv()
工作。这是我尝试过的:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('DBConn.php'); //My SQL server connection information
include 'Helper/LogReport.php'; //Keeps a count of how many times reports are exported
$query = $conn->query("SELECT QName, tsql from pmdb.QDefs WHERE QName = '" .$TableName. "'");
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
try
{
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
//$openFile = fopen('php://output','w');
$openFile = fopen($filepath . $filename,'w');
}
catch(Exception $e)
{
echo "Something went wrong<br>";
die( print_r( $e->getMessage()));
}
//define separators
$sep = ","; //separator
$br = "\r\n"; //line break
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
if(isset($DataReturn))
{
if(strpos($DataReturn['Order'],'EDIT'))
{
$DataReturn['Order'] = str_replace('EDIT','Id',$DataReturn['Order']);
}
$tsql = $tsql . $DataReturn['WhereClause'] . $DataReturn['Order'] . $DataReturn['Limit'];
}
$query = $conn->query($tsql);
$query->execute();
// Output data to CSV file
$headers = NULL;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
//Write column headings to file
if (is_null($headers))
{
$headers = array_keys((array)$row);
if ($headers[0] == 'ID')
$headers[0] = 'Id';
fputcsv($openFile, $headers, ',','"');
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', array_values($row));
$modRow = preg_replace( "/\r|\n/", "", $modRow );
/*
$modRow = str_replace('\r\n', " ", $modRow);
$modRow = str_replace('\n\r', " ", $modRow);
$modRow = str_replace('\n', " ", $modRow);
$modRow = str_replace('\r', " ", $modRow);
$modRow = str_replace(' ', " ", $modRow);
$modRow = str_replace('Â ', " ", $modRow);
$modRow = str_replace('"', '', $modRow);
$modRow = str_replace("'", "", $modRow);
*/
fputcsv($openFile, $modRow, ',','"');
}
// Close file
fclose($openFile);
但是文件中没有打印任何内容,它只是空白。我是不是设置错了什么?
编辑
我已经尝试了在 this page 上可以找到的 fopen
的所有变体,但在使用 fputcsv
时它们都给我一个空白文件。当我 echo
数组时,我只在文件中获取数据。
编辑结束
您可以看到 header 数组是根据从查询返回到数据库的数组键设置的。我可以 echo
他们并得到正确的 headers.
然后是数据行本身。我删除了不需要的字符,但它们仍然是数据数组,应该使用 fputcsv
打印。我可以再次 echo
通过遍历数组的内容。这就是我现在让导出工作的方式,但我知道这只是一种解决方法,我想让 fputcsv
工作。
这就是我打印行的方式:
foreach($modRow as $RowPrint)
{
echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;
更新
这是我 print_r
header 时的输出:
Array
(
[0] => Id
[1] => QSRC
[2] => QNAME
[3] => QDEF
[4] => ISACTIVE
[5] => RUNREPORT
[6] => FILEPATH
[7] => TSQL
)
这是我 print_r
$modRow
:
时的一行
Array
(
[Id] => 60
[QSRC] => bau
[QNAME] => Oops I deleted this!
[QDEF] => SELECT REGION
[ISACTIVE] => 0
[RUNREPORT] => 0
[FILEPATH] =>
[TSQL] =>
)
我 print_r
都在 fputcsv
之后应该将它们打印到文件中。这些是文件中唯一的内容。
我删除了第一个答案,因为它不正确。
*********************编辑*********************
您需要将此添加到文件末尾:
exit();
这会告诉浏览器您已完成对文件的写入,并确保出于安全原因不会向浏览器发送额外的内容。
您不能使用 php://stdout 将输出发送到浏览器。您可以使用 php://memory 来存储字符串,然后再读回。
$fd = fopen('php://memory','rw');
fwrite($fd, 'hey there'); //This would be your fputcsv stuff
fseek($fd, 0); //Reset the file pointer
$content = fread($fd, 4096); //This will read 4096 bytes maximum
echo $content;
我无法使 PHP built-in 函数 fputcsv()
工作。这是我尝试过的:
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('DBConn.php'); //My SQL server connection information
include 'Helper/LogReport.php'; //Keeps a count of how many times reports are exported
$query = $conn->query("SELECT QName, tsql from pmdb.QDefs WHERE QName = '" .$TableName. "'");
$query->execute();
$qdef = $query->fetch(PDO::FETCH_ASSOC);
// Create and open file for writing
$filepath = 'exports/';
$filename = $qdef['QName'] . '.csv';
try
{
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
//$openFile = fopen('php://output','w');
$openFile = fopen($filepath . $filename,'w');
}
catch(Exception $e)
{
echo "Something went wrong<br>";
die( print_r( $e->getMessage()));
}
//define separators
$sep = ","; //separator
$br = "\r\n"; //line break
// Use returned tsql field as query for dataset
$tsql = $qdef['tsql'];
if(isset($DataReturn))
{
if(strpos($DataReturn['Order'],'EDIT'))
{
$DataReturn['Order'] = str_replace('EDIT','Id',$DataReturn['Order']);
}
$tsql = $tsql . $DataReturn['WhereClause'] . $DataReturn['Order'] . $DataReturn['Limit'];
}
$query = $conn->query($tsql);
$query->execute();
// Output data to CSV file
$headers = NULL;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
//Write column headings to file
if (is_null($headers))
{
$headers = array_keys((array)$row);
if ($headers[0] == 'ID')
$headers[0] = 'Id';
fputcsv($openFile, $headers, ',','"');
}
//Write data
$modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', array_values($row));
$modRow = preg_replace( "/\r|\n/", "", $modRow );
/*
$modRow = str_replace('\r\n', " ", $modRow);
$modRow = str_replace('\n\r', " ", $modRow);
$modRow = str_replace('\n', " ", $modRow);
$modRow = str_replace('\r', " ", $modRow);
$modRow = str_replace(' ', " ", $modRow);
$modRow = str_replace('Â ', " ", $modRow);
$modRow = str_replace('"', '', $modRow);
$modRow = str_replace("'", "", $modRow);
*/
fputcsv($openFile, $modRow, ',','"');
}
// Close file
fclose($openFile);
但是文件中没有打印任何内容,它只是空白。我是不是设置错了什么?
编辑
我已经尝试了在 this page 上可以找到的 fopen
的所有变体,但在使用 fputcsv
时它们都给我一个空白文件。当我 echo
数组时,我只在文件中获取数据。
编辑结束
您可以看到 header 数组是根据从查询返回到数据库的数组键设置的。我可以 echo
他们并得到正确的 headers.
然后是数据行本身。我删除了不需要的字符,但它们仍然是数据数组,应该使用 fputcsv
打印。我可以再次 echo
通过遍历数组的内容。这就是我现在让导出工作的方式,但我知道这只是一种解决方法,我想让 fputcsv
工作。
这就是我打印行的方式:
foreach($modRow as $RowPrint)
{
echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;
更新
这是我 print_r
header 时的输出:
Array
(
[0] => Id
[1] => QSRC
[2] => QNAME
[3] => QDEF
[4] => ISACTIVE
[5] => RUNREPORT
[6] => FILEPATH
[7] => TSQL
)
这是我 print_r
$modRow
:
Array
(
[Id] => 60
[QSRC] => bau
[QNAME] => Oops I deleted this!
[QDEF] => SELECT REGION
[ISACTIVE] => 0
[RUNREPORT] => 0
[FILEPATH] =>
[TSQL] =>
)
我 print_r
都在 fputcsv
之后应该将它们打印到文件中。这些是文件中唯一的内容。
我删除了第一个答案,因为它不正确。
*********************编辑*********************
您需要将此添加到文件末尾:
exit();
这会告诉浏览器您已完成对文件的写入,并确保出于安全原因不会向浏览器发送额外的内容。
您不能使用 php://stdout 将输出发送到浏览器。您可以使用 php://memory 来存储字符串,然后再读回。
$fd = fopen('php://memory','rw');
fwrite($fd, 'hey there'); //This would be your fputcsv stuff
fseek($fd, 0); //Reset the file pointer
$content = fread($fd, 4096); //This will read 4096 bytes maximum
echo $content;