Excluir arquivo não gerenciado após download?
-
29-09-2020 - |
Pergunta
Em um módulo personalizado eu crio um arquivo e uso drupal_goto
para baixá-lo.Como posso excluir o arquivo após o download?Colocando um file_unmanaged_delete
depois de drupal_goto
não é executado e hook_file_download
acontece antes do download para verificações de permissões e coisas assim.Então não tenho certeza de como fazer isso.Preciso recorrer à limpeza de arquivos em uma pasta com o cron de alguma forma?
Nenhuma solução correta
Outras dicas
// 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;
O código a seguir removerá o arquivo após o download, considerando que seu arquivo está localizado no diretório 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();
}
Você pode visitar o seguinte link para obter detalheshttp://kahthong.com/2012/08/simple-example-how-use-drupal-serve-file-downloads
Obrigado!
Licenciado em: CC-BY-SA com atribuição
Não afiliado a drupal.stackexchange