在自定义模块中,我创建一个文件,然后使用 drupal_goto 下载它。文件下载后如何删除?放置一个 file_unmanaged_delete 之后 drupal_goto 没有被执行,并且 hook_file_download 在下载之前进行权限检查等。所以我不知道该怎么做。我是否必须以某种方式使用 cron 来清理文件夹中的文件?

没有正确的解决方案

其他提示

// Generate pdf card.
$file_path = $crm_card->generatePdfCard();
$filename = basename($file_path);
$headers = [
  'Content-Type' => 'application/octet-stream',
  'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  'Content-Length' => filesize($file_path),
];

$binary_file_response = new BinaryFileResponse($file_path, 200, $headers);
$binary_file_response->deleteFileAfterSend(TRUE);

return $binary_file_response;

考虑到您的文件位于 tmp 目录中,以下代码将在下载后删除该文件。

$filename = 'foobar.xls';
$temp_path = realpath(file_directory_temp()) . '/';
if (file_exists($temp_path . $filename)) {
  // Serve file download.
  drupal_add_http_header('Pragma', 'public');
  drupal_add_http_header('Expires', '0');
  drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  drupal_add_http_header('Content-Type', 'application/vnd.ms-excel');
  drupal_add_http_header('Content-Disposition', 'attachment; filename=' . basename($temp_path . $filename) . ';');
  drupal_add_http_header('Content-Transfer-Encoding', 'binary');
  drupal_add_http_header('Content-Length', filesize($temp_path . $filename));
  readfile($temp_path . $filename);
  unlink($temp_path . $filename);
  drupal_exit();
}

您可以访问以下链接了解详细信息http://kahthong.com/2012/08/simple-example-how-use-drupal-serve-file-downloads

谢谢!

许可以下: CC-BY-SA归因
scroll top