Starting a MP4 download from a php script

So I have this small PHP condition for a PHP download, it's been working fine for weeks then all of a sudden I noticed it's stopped working.

Rather than getting a download of the file, the users are getting a 0kb file instead.

I have checked the URL and it's still working as expected.

Here is the code

if ($refer == 'stackoverflow.com' || $refer == 'dev.stackoverflow.com') {

        $fichero = $downloadUrl;

        if ($fichero) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($fichero));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($fichero));
                ob_clean();
                flush();
                readfile($fichero);
                exit;
        } else {
                die("The File $fichero does not exist");
        }
} else {
        die("Sorry you must start the download from the website");
}

Now I know it's hitting the conditionals, because I am not getting any of the die statements and the file is still being offered as a download. This seems to have broken in Chrome and also IE(Edge). I have been doing some reading regarding headers and the only documentation on this proble I can find that appears to be relevant is the expires setting which i set to 0.

Am I missing an easy trick here?

Thanks a lot!

Upvotes: 0

Views: 1248

Answers (1)

header('Content-Length: ' . filesize($fichero));

Was causing the issue, I should have debugged this a little more before posting.

This file needed write in order to suppress this warning

PHP Warning: filesize(): stat failed for

When removing

header('Content-Length: ' . filesize($fichero));

The code worked as expected, I tweaked with chmod on the file and re-enabled the above code and it fixed my issue.

Thanks

Upvotes: 1

Related Questions