简体   繁体   中英

How can i do something after my header in php?

I want that my script will be executed after downloading a file so in the first part of this code it will get the.txt file and change word license to the database result, then i want that the download starts and after that i want to clear the.txt file for the next use. If i write it as below i don't get the database result in the text file because it executes first the whole code before it downloads. If i remove the last part it all works but it wont reset the text.

<?php

$userID= $_SESSION['user_id'];
$license=$dbConnection->getOne("SELECT license FROM valid_license where discordid = '$userID' ");
$license2 = $license['license'];

$zip = new ZipArchive;
$fileToModify = 'license.txt';
if ($zip->open('test.zip') === TRUE) {
    $oldContents = $zip->getFromName($fileToModify);
    $newContents = str_replace('license', $license2, $oldContents);
    $zip->deleteName($fileToModify);
    $zip->addFromString($fileToModify, $newContents);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

header("Location: test.zip");

$userID= $_SESSION['user_id'];
$license=$dbConnection->getOne("SELECT license FROM valid_license where discordid = '$userID' ");
$license2 = $license['license'];

$zip = new ZipArchive;
$fileToModify = 'license.txt';
if ($zip->open('test.zip') === TRUE) {
    $oldContents = $zip->getFromName($fileToModify);
    $newContents = str_replace($license2, 'license', $oldContents);
    $zip->deleteName($fileToModify);
    $zip->addFromString($fileToModify, $newContents);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

?>

There are two potential reasons why this is happening:

  1. Your web server is waiting for PHP to finish executing before serving any response to the user, so the second half is overwriting the zip file before the header is ever sent.
  2. Your web server is sending the Location: header without delay, but also your PHP code is still executing while that response and the subsequent request are in-flight, overwriting the data before the request for the file comes back.

Either of those will break your intended flow.

Instead of using a Location: header, set the appropriate Content-Type: header for a zip file, dump the data out to the user, and then clean up the file.

header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);

Additionally, do not modify the original zip file like this. If you get two overlapping requests you'll wind up either serving the wrong license, or just corrupt the file for one or both or all subsequent.

Make a copy, modify the copy, serve the copy, delete the copy.

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