1

This is my code:

error_log('download start');
readfile('setup.exe');
error_log('download complete');

The file was received successfully but the third line is not executed, its not log the 'download complete' We facing this problem for a long time we tried also to send chunks (using flush) of this file and after several chunks the connection lost so the rest executable code is not running (but the file received). We know that the connection is lost by using connection handling in php. We also set connection time limit to max.

Ben
  • 249
  • 5
  • 19
  • I use [This one](http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file) because it's far easier for doing such things and it supports resume! – undone Jun 12 '12 at 08:54
  • @Death thanks, i just tried all the solutions in the link you supplied but still have the problem. it looks like it disconnect after specific amount of bytes without any reason but the file still sent successfully and the rest of the code is not executed. – Ben Jun 12 '12 at 09:19
  • can you test it with `file_put_content` and see what happens? – undone Jun 12 '12 at 10:29
  • @Death same deal, still have the problem. – Ben Jun 12 '12 at 11:11

1 Answers1

0

You need to set certain headers first, binary, etc:

$file = 'setup.exe';

if (file_exists($file)) {
    error_log('download start');
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    $result = readfile($file);

    if ($result) {
       error_log('download complete');
    }
    else {
       error_log('unable to download');
    }

    exit;
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578