0

I'd like to know if I can run code after a location header is done ? My code does the unlink function before the header has finished.

header('HTTP/1.1 307 Temporary Redirect');
header("Location: $file_url");
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
header('Pragma: no-cache');

unlink($fileName);'''

The unlink function is made before the file download which is not good for what I try to do.

Any ideas?

Amaury Leproux
  • 229
  • 1
  • 5
  • 21
  • 1
    From the duplicate *The header is only a line of data asking the browser to redirect*, so all of the remaining script is run. – Nigel Ren Mar 09 '21 at 16:28
  • 1
    You need to have a separate script to delete the file at some later time. Maybe a Cron job which tidies up the folder periodically. Either that or perhaps you could output the content to the browser direct rather than saving it to disk first, so there's never a file to begin with. Whether that's actually feasible for you though depends on a lot of context about how this overall process works in your application - how the file is created, where the content of it originates from, whether the creation occurs in the same request as the download, etc. – ADyson Mar 10 '21 at 16:25
  • 1
    P.s. the header is merely an instruction to the browser which is delivered as part of the response, so `My code does the unlink function before the header has finished` is inaccurate. The header doesn't do anything by itself. the browser may act on it once it receives it. `The unlink function is made before the file download` is a more accurate description of the situation though. – ADyson Mar 10 '21 at 16:29

0 Answers0