php下载不一致的行为

i have a little download script setup here and it works well SOMETIMES. I have narrowed down the problem to the response headers being sent only majority of the times and when they dont what happens is that it asks me to download the file giving no information about it except for its name on disk and that its being downloaded as as 'application/octet-stream'. here are the headers i am sending

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$file->logical_name.'"');
header('Content-Type: "'.$mime.'"');
header("Content-Length: ".filesize($this->file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
readfile($this->file);  

so to be clear about my issue the file information is only being sent sometimes and when they dont i get my issue, which is no information about the file being sent hence it being downloaded as an weired named(as on disk) 'application/octet-stream'

UPDATE

this dosnt happen with smaller files around less than 100kb

flushing all the output before reading the file solves the problem. seems the key was ensure all those headers were actually sent before tyring to download the file (reading the file with readfile())

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$file->logical_name.'"');
header('Content-Type: "'.$mime.'"');
header('Content-Length:'.filesize($this->file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
flush();
readfile($this->file);

adding the flush() function solved the problem. Note i didnt start a buffer with ob_start so i didnt do ob_flush but in the case you did this that might do it