Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
使用命令行中的fputcsv(而不是浏览器)从数组(php)导出csv文件_Php_Arrays_Csv_Fputcsv - Fatal编程技术网

使用命令行中的fputcsv(而不是浏览器)从数组(php)导出csv文件

使用命令行中的fputcsv(而不是浏览器)从数组(php)导出csv文件,php,arrays,csv,fputcsv,Php,Arrays,Csv,Fputcsv,我已经编写了一个代码,将数组的数据导出到csv文件中。但它只在我在浏览器中打开脚本时起作用。我需要一个脚本,我可以运行在突击队行,因为一个cronjob。我只能显示脚本的最后一部分,其余部分用于其他事情 。。。。。。。。。。。。。 ............. $arrayCSV[]= array ( "id" => $templateID, "state" => $templateState, "price" => $templatePri

我已经编写了一个代码,将数组的数据导出到csv文件中。但它只在我在浏览器中打开脚本时起作用。我需要一个脚本,我可以运行在突击队行,因为一个cronjob。我只能显示脚本的最后一部分,其余部分用于其他事情

。。。。。。。。。。。。。 .............

$arrayCSV[]= array (
      "id" => $templateID,
      "state" => $templateState,
      "price" => $templatePrice,
      "exc_price" => $templateExc_price,
      "downloads" => $templateDownloads,
      "inserted_date" => $templateInsertedDate,
      "update_date" => $templateUpdateDate,
      "type" => $templateType,
      "author" => $templateAuthor,
      "live_preview_url" => $templateLivePreview,
      "screenshot_big" => $templateScreenshotBig,
      "screenshot_original" => $templateScreenshotOriginal,
      "screenshot_german" => $templateScreenshotGerman,
      "screenshot_responsive" => $templateScreenshotResponsive,
      "keywords" => $templateKeywords,
      "keywords_german" => $templateKeywordsGerman,
      "categories" => $templateCategories,
      "sources" => $templateSources,
      "features" => $templateFeatures,
      "template_name" => $templateName,
  );

};
$csvExportFile = 'gettemplates.csv';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$csvExportFile");
$output = fopen("php://output", "w");
$header = array_keys($arrayCSV[0]);
fputcsv($output, $header);
foreach($arrayCSV as $row){
    fputcsv($output, $row);
}
fclose($output);

它在浏览器中工作,但我需要更改它,它只从中写入文件…

如果您需要csv文件中的标题,只需将其添加到
arrayCSV[]
,如下所示,然后写入您的csv文件:

$arrayCSV["header"] = "Content-type: text/csv";
$csvExportFile = 'gettemplates.csv';
$output = fopen($csvExportFile, "w");
foreach($arrayCSV as $row){
    fputcsv($output, $row);
}
fclose($output);

你需要输出到一个文件,而不是输出到。你不需要标题。是的,我想我明白了。但是我是否需要将头放在文件中,或者我只需要写$output=fopen($csvExportFile,“w”);标题是对浏览器的说明:嘿,浏览器,这是一个CSV文件。嘿,浏览器,这是一个名为“gettemplates.csv”的附件。你不需要它们。只要做:
$output=fopen($csvExportFile,“w”)(但请考虑文件将保存的位置)。您是对的,我只更改了输出,它工作正常!谢谢你的提示<代码>$csvExportFile='gettemplates.csv'$输出=fopen($csvExportFile,“w”)$header=array_键($arrayCSV[0]);fputcsv($output,$header);foreach($arrayCSV作为$row){fputcsv($output,$row);}fclose($output)