php - Drupal6 - 使用 MENU_CALLBACK 输出 CSV

标签 php drupal drupal-6 csv drupal-modules

我想使用 drupal 6 模块输出 csv 文件。这是我拥有的代码,但它与我的自定义主题中的一些代码以及我的模块中的一些代码一起被黑客攻击。我是否可以将其全部移至我的模块中?

///////////////////////////
// csv.module <- a custom module
///////////////////////////
function csv_menu() {

    $items = array();

    $items['csv/results'] = array (
        'page callback' => 'csv_results_page',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function csv_theme() {
    $items = array();

    $items['csv_results'] = array(
        'arguments' => array(),
    );

    return $items;
}

function csv_results_page() {
    return generate_csv_results();
}

function theme_csv_results() {
    return generate_csv_results();
}

function generate_csv_results() {
    return "header1,header2,header3,header4\nval1,val2,val3,val4\n";
}


//////////////////////////////
// page-csv-results.tpl.php <- in my theme. I would like it all contained in the module.
//////////////////////////////
<?php
    //!TODO: Change Content Type Header
    print theme('csv_results');

编辑

以下是针对有类似问题的任何人的更新版本。感谢 chx!

///////////////////////////
// csv.module <- a custom module
///////////////////////////
function csv_menu() {

    $items = array();

    $items['csv/results'] = array (
        'page callback' => 'csv_results_page',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function csv_results_page() {
    //Take a look at the Nikit's response on header stuff. This might be incorrect.
    header('Content-Type: text/x-comma-separated-values');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Cache-Control: private',false); // required for certain browser
    print generate_csv_results();
}

function generate_csv_results() {
    return "header1,header2,header3,header4\nval1,val2,val3,val4\n";
}

最佳答案

这是我导出csv文件的简化版本,将此代码添加到csv_results_page()中。这里使用查询,但也可以是其他东西(如数组、文件列表等):

/**
 * Export to cvs
 */
function _YOURMODULENAME_csv_export() {
  $delimiter = "\t"; // This is tab delimiter, can be other

  $temp = file_directory_temp();

  $file = tempnam(realpath($temp), 'csv');
  if (!$fp = fopen($file, 'a')) {
    drupal_set_message(t('The file for exported could not be created. Refer to the site administrator'), 'error');
    return;
  }

  $title = "Some title for csv\n"; // you can remove it, if don't want title in csv

  $title .= implode($delimiter, array('Column name 1','Column name 2')). "\n"; // Add all columns titles here, it should mutch query result fields below

  fwrite($fp, $title);

  $query = 'WRITE HERE SOME CODE THAT SHOULD RESULT QUERY AND  RETURN fields in order described in $title above';
  $result = db_query($query);
  while ($data = db_fetch_array($result)) {
    $rows = implode($delimiter, $data);
    fwrite($fp, $rows."\n");
  }

  fclose($fp);

  $header = array();

  $header[] = 'Pragma: no-cache';
  $header[] = 'Cache-Control: no-cache, must-revalidate';
  $header[] = 'Content-Disposition: attachment; filename="exort_'.date('Ymd',time()).'.csv";'; // This is file name
  $header[] = 'Content-Type: text/x-comma-separated-values';
  $header[] = 'Content-Length: '.filesize($file);
  $header[] = 'Connection: close';

  file_transfer($file, $header);
  file_delete($file);

  return;
}

关于php - Drupal6 - 使用 MENU_CALLBACK 输出 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021178/

相关文章:

mysql - 查找 MySQL 中每个数据库的事件连接数

php - 如何查找包含特定字段的所有节点类型?

mysql - 导入现有的 Drupal 项目

php - Magento - 仅加载可配置产品

php - INSERT 命令插入重复项

css - Drupal block Twig 文件

php - 如何将数组传递给 Drupal 菜单回调

php - 分组顺序数据

php - Codeception - 如何处理具有非唯一名称的字段

html - drupal img 标签未使用干净的 url 显示