Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to browse the site while downloading a file using php

I used this code for downloading files

$file='test.mp3';
$download_rate = 50; //50 kb/s

if(file_exists($file) && is_file($file))
{
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($file));
    header('Content-Disposition: filename='.$file);

    flush();
    $file = fopen($file, "r");

    while(!feof($file))
    {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
        // flush the content to the browser
        flush();
        // sleep one second
        sleep(1);
    }
    fclose($file);
    }
else {
    echo 'File Not Found';
}

but while downloading the file cannot browse the site till the download completed. this happened with IE and Firefox

Any answers?

like image 568
Khaled Avatar asked Oct 21 '25 04:10

Khaled


1 Answers

Only time I know this happens is when you have sessions which have not been written. I can't see any sessions here so I'm not sure what is causing it. However, most php download file scripts are used to check for logins so I'm guessing this is the case. if you do have sessions, try session_write_close();

like image 180
frostymarvelous Avatar answered Oct 22 '25 16:10

frostymarvelous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!