Eliminare il file non gestito dopo il download?
-
29-09-2020 - |
Domanda
In un modulo personalizzato creo un file e quindi utilizzo drupal_goto
per scaricarlo.Come posso eliminare il file dopo che è stato scaricato?Posizionando a file_unmanaged_delete
dopo il drupal_goto
non viene eseguito, e hook_file_download
avviene prima del download per controlli di permessi e simili.Quindi non sono sicuro di come farlo.Devo ricorrere in qualche modo alla pulizia dei file in una cartella con cron?
Nessuna soluzione corretta
Altri suggerimenti
// 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;
Il seguente codice rimuoverà il file dopo il download considerando che il file si trova nella directory 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();
}
È possibile visitare il seguente collegamento per i dettaglihttp://kahthong.com/2012/08/simple-example-how-use-drupal-serve-file-downloads
Grazie!
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a drupal.stackexchange