我想输出带有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";
}
答案 0 :(得分:2)
这是我导出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;
}
答案 1 :(得分:1)
输出CSV应该只通过在页面回调中打印内容来完成,如果你return
,那么它将在当前主题中,被HTML包围,你不需要它。