Header redirect to file, wait, then unlink same file. why does it run backwards?

simple enough, right? I run this:

// copy file to stage folder
copy($pathtofile.“/”.$nameandext, $_SERVER[‘DOCUMENT_ROOT’].“/files/box/stage/”.$nameandext);

header(“location: /files/box/stage/”.“$nameandext”); // send copy to browser
usleep(2000000); // wait
unlink($_SERVER[‘DOCUMENT_ROOT’].“/files/box/stage/”.$nameandext); // destroy copy in stage folder

and I get this: wait, delete, 404! (or is it delete, wait, 404!) I trust copy is happening, because…

all works great without wait and delete

  • but I need to destroy copy immediately!

WTF… any ideas why this is happening please!!

header("location: /files/box/stage/"."$nameandext"); // send copy to browser

header function will just send header information to browser, it is not sending complete file to browser.

Try to use the below code, surely it will work and i have tested it.

header('Content-type: text/plain'); // change this content type based on your file extension
header('Content-Disposition: attachment; filename="'.$nameandext.'"');




hi Tamil!

yes, that would explain things…

it’s late here and will have a go at your suggestion tomorrow.

much thanks for your time and expertise Tamil !!

well getting pretty close. Tamil’s suggested code below working EXCEPT no image presented. just an empty box of proper size with the little X in the corner…

header(‘Content-type: image/jpeg’); // change this content type based on your file ext
header(‘Content-Disposition: inline; filename="’.$newpath.$nameandext.‘"’);

now I’m going in circles. the problem above is why I switched from non-working readfile(), fopen(), fpassthru(), to the WORKING header(location) redirect !!!

why is that image getting lost???
OR is there some way to force completion of header(location) redirect, then unlink the same file ???
any thoughts at all PLEASE !!