1

I am a beginner. I've searched everywhere, but I don't know why after downloading a file from the server through the client (Win 7 Firefox), I am unable to open the file. I've tried a PNG file and an MP4 file. The download completes, but the files don't open. Here's my script;

$dl_file = $_GET['val']; //Verified the full path and the file name gets passed here
$basename = basename($dl_file);
$ext = pathinfo($dl_file, PATHINFO_EXTENSION);
$length   = sprintf("%u", filesize($dl_file));

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$basename.'');
//manually tried '.$basename.'.PNG' - DID NOT work. How to pass $ext here?
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . $length);

        set_time_limit(0);
        readfile($dl_file);

I can't imagine why the downloaded file won't open. Is it corrupt? Please throw more light on this. Thank you in advance.

4
  • What do you mean by won't open? Do the completed file sizes look right? Have you tried downloading a simple text file and opening it? Maybe try putting "hello world" in a text file, and downloading that file.
    – light
    Commented Jun 25, 2015 at 6:19
  • The download completes, but the files don't open with the players. A video file (19.7M), 1.mp4 downloads. It's saved on my HD. I open with VLC and it doesn't. I am only hosting image and video files. Downloaded PNG file. Opened with Win Photo Viewer. It displays an error. "Doesn't support format".
    – Zac1
    Commented Jun 25, 2015 at 7:12
  • It'd be hard to tell why the files got corrupted, or even whether they were corrupted to begin with. The only way we can ascertain it is something wrong with your code, and to have a hint as to why, would be to do a simple test as I described. It shouldn't be hard to just put a text file in the server, then download it to see what happens.
    – light
    Commented Jun 25, 2015 at 7:40
  • Try copying the code from example 1 and see if that works: php.net/manual/en/function.readfile.php
    – Drakes
    Commented Jun 25, 2015 at 12:56

1 Answer 1

5

after many tries, I found that adding the 2 lines of code shown below (ob_clean, and flush) (and/or adding precheck, postcheck parameters) - worked on all browsers.Thanks.

if (file_exists($dl_file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($dl_file).'"' );
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($dl_file));
    ob_clean();
    flush();
    readfile($dl_file);
    exit;
}
2
  • 1
    Thanks, i have added ob_clean(); flush();. It worked for me. Commented Nov 17, 2016 at 14:02
  • 1
    I was searching the solutions for same kind of problem, this helped me a lot @Zac1 . Thanks Commented Jul 30, 2017 at 12:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.