War Coder
War Coder

Reputation: 464

problem downloading file

I write a script to force download mp3 files from a site. The code is working very fine but the problem is that it can't download large files. I tried it with a file of 9.21mb and it downloaded correctly, but whenever i try to use the code to download a file of 25mb, it simply gives me a cannot find server page or The website cannot display the page. So i now know it has problems downloading large files. Below is the code snippet that does the downloading of files.

header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);             
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$dname.".mp3\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($secretfile));
$downloaded=readfile($secretfile);  

The displayed error is: HTTP 500 Internal Server Error

thank u very much for ur time guys.

Upvotes: 1

Views: 1733

Answers (4)

Gumbo
Gumbo

Reputation: 655239

Try this:

// empty output buffer
while (ob_get_level()) {
    ob_end_clean();
}
if (ini_get('output_buffering')) {
    ini_get('output_buffering', false);
}

// function to encode quoted-string tokens
function rfc2822_quoteString($string) {
    return '"'.preg_replace('/[^\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\x7F]/', '\\\$0', $string).'"';
}

// HTTP headers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.rfc2822_quoteString($dname.'.mp3'));
header('Content-Length: '.filesize($secretfile));

// send file
readfile($secretfile);
exit;

Upvotes: 0

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67147

i simply gives me a cannot find server page or The website cannot display the page

Is this the error as displayed by Internet Explorer? Do you get any server-side errors? Did you check your server logs?

Upvotes: 0

David Weitz
David Weitz

Reputation: 461

It could be memory limits, but usually PHP will output an error saying that the memory limit has been reached.

Also, before all of that you should disable output compression if it's enabled:

if(ini_get('zlib.output_compression')) {
    ini_set('zlib.output_compression', 'Off');
}

Sometimes IE can screw up if output compression is enabled.

Upvotes: 2

Julien Roncaglia
Julien Roncaglia

Reputation: 17837

Watch your PHP configuration for memory limits and timeouts

In php.ini :

memory_limit = 32M
max_execution_time = 300

Note that if you want to go really high in execution time you also need to change your web server timeout.

Upvotes: 0

Related Questions