Удалить неуправляемый файл после загрузки?

drupal.stackexchange https://drupal.stackexchange.com/questions/179061

  •  29-09-2020
  •  | 
  •  

Вопрос

В пользовательском модуле я создаю файл, а затем использую 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 с атрибуция
Не связан с drupal.stackexchange
scroll top