ダウンロード後に管理されていないファイルを削除しますか?

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