Question

Dans un module personnalisé, je crée un fichier puis j'utilise drupal_goto pour le télécharger.Comment puis-je supprimer le fichier une fois téléchargé ?Placer un file_unmanaged_delete après le drupal_goto n'est pas exécuté, et hook_file_download se produit avant le téléchargement pour les vérifications d'autorisations et autres.Je ne sais donc pas comment procéder.Dois-je recourir au nettoyage des fichiers dans un dossier avec cron d'une manière ou d'une autre ?

Pas de solution correcte

Autres conseils

// 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;

Le code suivant supprimera le fichier après le téléchargement étant donné que votre fichier se trouve dans le répertoire 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();
}

Vous pouvez visiter le lien suivant pour plus de détailshttp://kahthong.com/2012/08/simple-example-how-use-drupal-serve-file-downloads

Merci!

Licencié sous: CC-BY-SA avec attribution
Non affilié à drupal.stackexchange
scroll top