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

i have write a code to export Data of an array to a csv file. But it only works, when i open the script in browser. I need a script that i can run in commando line because a cronjob. I can only show the last part of the script, the rest is for other things...

............. .............

$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);

it works in browser, but i need to change it, that it only writes a file from it...

If you need the header in the csv file, just add it to the arrayCSV[], as follows, and write to your csv file:

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