Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在服务器上保存导出的文件。它应该在系统中下载,而不保存在服务器上_Php_Wordpress_Csv_Fwrite_Fclose - Fatal编程技术网

Php 在服务器上保存导出的文件。它应该在系统中下载,而不保存在服务器上

Php 在服务器上保存导出的文件。它应该在系统中下载,而不保存在服务器上,php,wordpress,csv,fwrite,fclose,Php,Wordpress,Csv,Fwrite,Fclose,我有PHP脚本将记录从数据库导出到CSV文件。当我点击导出按钮时,它会在服务器上创建一个CSV文件(当前位置)。所有数据均正确显示在CSV文件中,但不应保存在服务器上,应在系统中下载。这个问题可能是重复的,但我没有找到答案 CSV文件中导出数据的代码 $monthData = mysql_query($query, $conn); $columns_total = mysql_num_fields($monthData); $output = ""; for ($i = 2; $i <

我有PHP脚本将记录从数据库导出到CSV文件。当我点击导出按钮时,它会在服务器上创建一个CSV文件(当前位置)。所有数据均正确显示在CSV文件中,但不应保存在服务器上,应在系统中下载。这个问题可能是重复的,但我没有找到答案

CSV文件中导出数据的代码

$monthData  = mysql_query($query, $conn);
$columns_total = mysql_num_fields($monthData);

$output = "";

for ($i = 2; $i < $columns_total; $i++) {
    $heading = mysql_field_name($monthData,$i);
    $output .= '"'.$heading.'",';
}
$output .="\n";

while ($row = mysql_fetch_array($monthData)) {
    for ($i = 2; $i < $columns_total; $i++) {
        $output .='"'.$row["$i"].'",';
    }
    $output .="\n";
}
$fileName = $month.'Data.csv';
$csvfile = fopen($fileName, "w") or die("Unable to open file!");
fwrite($csvfile, $output);

fclose($csvfile);
$monthData=mysql\u查询($query,$conn);
$columns\u total=mysql\u num\u字段($monthData);
$output=“”;
对于($i=2;$i<$columns_total;$i++){
$heading=mysql\u field\u name($monthData,$i);
$output.='''.$heading'.''.'',';
}
$output.=“\n”;
while($row=mysql\u fetch\u数组($monthData)){
对于($i=2;$i<$columns_total;$i++){
$output.=''''.$row[“$i”].'”,';
}
$output.=“\n”;
}
$fileName=$month.Data.csv';
$csvfile=fopen($fileName,“w”)或die(“无法打开文件!”);
fwrite($csvfile,$output);
fclose($csvfile);
根据我的研究或知识,我认为有必要添加一些标题(指定文件名、文件大小和许多其他)。我没有太多的知识,为什么我需要添加它,我需要在哪里添加它


有人能帮我吗?

给你问题的简单答案

<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=testdata.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// fetch the data
$row = array(array('row1val','row1val','row1val'),array('row2val','row2val','row2val')); 

// loop over the rows, outputting them
foreach($row as $fields):
fputcsv($output, $fields);
endforeach;

fclose($output);

?>


谢谢

$csvfile=fopen($fileName,“w”)
更改为
$csvfile=fopen($php://output“,”w“
。。。。在做这件事之前先发送。。。。我发布的链接中的示例展示了如何发送pdf文件的标题,但是您应该能够轻松地将内容类型更改为
text/csv
instead@MarkBaker-谢谢你的回复。现在,即使在服务器上也无法下载该文件。它向我显示警告-无法修改标题信息-标题已由………..发送…以正确方式使用标题可能是我的错误。你能把语法贴出来吗。很紧急。您必须先发送邮件头,然后才能将任何内容发送到
php://output
。。。如果您在打开文件之前收到了带有标题的特定消息,那么代码中的其他内容正在向浏览器发送输出,并且错误消息应该准确地告诉您如何将导出的文件保存在服务器上?