简体   繁体   中英

Remove include file in php script

I have a php file to handle file downloads in a website. It is working as it should.

if($_GET['type'] == "pdf") {
    $dir = __DIR__ . "/../src/files/pdf/" . $_GET['name'];
    if(file_exists($dir)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename='.basename($dir));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($dir));
        readfile($dir);
        exit;
    } else {
        $_SESSION['err'] = "No file found";
        header("location: index.php");
        exit;
    }
}

Now I want to count the times that a file is being downloaded. So I have a method inside a class in a different php file. I usually include this php file and create an instance of this class when I need to use some of its methods. The problem is that if I include the file in my file-handler.php the header will be different and the file would be corrupted when downloaded. So, how can I use the method of this file in the file-handler without affecting the header?

If you include your counting file before at the top of file-hander.php file before you output your download headers then the download headers will replace whatever headers were output before.

Additionally. your counting file must not output any content at all as the client is expecting the file you are downloading and nothing else.

I agree 100% with @rjdown, your code creates a massive potential security issue. I would not put it on a server connected to an untrusted network (Like The Internet). If you would appreciate help making it secure then please ask that question.

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