duanqiu3800 2019-07-22 14:53
浏览 239

在命令行中使用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...

  • 写回答

1条回答 默认 最新

  • dprnr5559 2019-07-22 15:06
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号