简体   繁体   中英

Why some code will not be ran after the header(“Content-disposition: …”) line?

I was wondering if anyone know why the code after the header(Content-disposition: ...) will not run? It writes and downloads the file, but it will not run the unlink() line...

This code is within a few other if statements originally.

Here is the code:

$form1Array = array($_POST["namn"], $_POST["stad"]);
$form1String = serialize($form1Array);
$form1Fil = $_POST["namn"].".txt";
$midMappe = "midlertidig\\";
$fo_form1Fil = fopen($midMappe.$form1Fil, "w");
//
if (fwrite($fo_form1Fil, $form1String)) {
    ignore_user_abort(true);
    //
    header("Content-type: text/plain");
    header("Content-disposition: attachment; filename=".$form1Fil);
    //
    unlink($midMappe.$form1Fil);
    } else {
       echo "Could not save the file for download.";    
    }

Thank you. :)

The code will be correctly executed and as someone pointed out in the comments, it is very likely that the problem is in the unlink() function. I recommend you to do a quick debug and see what unlink() returns. More in particular, see if it return false (it will if it fails to unlink the file).

Here an example of what i mean

I figured it out with the help of 'CBroe', thank you.

I also added a connection_abort() line which I found out about here .

Here is the updated code:

$form1Array = array($_POST["namn"], $_POST["stad"]);
$form1String = serialize($form1Array);
$form1Fil = $_POST["namn"].".txt";
$midMappe = "midlertidig\\";
$fo_form1Fil = fopen($midMappe.$form1Fil, "w");
//
if (fwrite($fo_form1Fil, $form1String)) {
    fclose($fo_form1Fil); // ** Here is the updated/added line 1 **
    //
    ignore_user_abort(true);
    //
    header("Content-type: text/plain");
    header("Content-disposition: attachment; filename=".$form1Fil);
    //
    if (connection_abort()) { // ** Here is the updated/added line 2 **
        unlink($midMappe.$form1Fil);
    } else {
        unlink($midMappe.$form1Fil);
    }
    } else {
       echo "Could not save the file for download.";    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM