【发布时间】:2018-03-22 13:40:48
【问题描述】:
我有一份报告,我想让用户将当前数据提取到可下载的 .csv 文件中。
我目前正在使用 unmanaged_write_submit 但这只是将 csv 打印到屏幕上,用户必须右键单击并另存为。我希望通过单击按钮下载实际文件来使其尽可能简单。
$page['submit'] = array(
'#type' => 'submit',
'#value' => 'Download Report',
'#submit' => array('test_unmanaged_write_submit'),
);
function test_unmanaged_write_submit($form, &$form_state) {
//$data is pulling the .csv/query data
$data = get_array_data($form,&$form_state);
$destination = !empty($form_state['values']['destination']) ? $form_state['values']['destination'] : NULL;
// With the unmanaged file we just get a filename back.
$filename = file_unmanaged_save_data($data, $destination, FILE_EXISTS_REPLACE);
if ($filename) {
$url = file_create_url($filename);
$_SESSION['file_example_default_file'] = $filename;
drupal_set_message(t('Saved file as %filename (accessible via !url, uri=<span id="uri">@uri</span>)', array(
'%filename' => $filename,
'@uri' => $filename,
'!url' => l(t('this URL'), $url),
)));
}
else {
drupal_set_message(t('Failed to save the file'), 'error');
}
}
但这仅提供指向在屏幕上显示 .csv 的网页的链接。是否有另一种 drupal 函数或方法可以强制在 php 中下载文件?
【问题讨论】: