tags:

views:

151

answers:

4

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.

A: 

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.

VirtualBlackFox
Thank you very much for your quick response. The site is hosted on a shared hosting package with aplus.netIf i am able to edit those parameters, what should be the appropriate one i should replace it with. thanks
War Coder
+1  A: 

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.

David Weitz
thanks 4 the advise. i will disable the output compression as advised. concerning the error thing, it might not have displayed an error coz i set error_reporting(0) . will set it to display all errors and will let u know if i can any.
War Coder
modified the code and disabled compression and it worked. thanks man
War Coder
A: 

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?

Ferdinand Beyer
The displayed error is: HTTP 500 Internal Server Error
War Coder
Well then check your server logs to see what server-side PHP error has occurred!
Ferdinand Beyer
Oh, make sure to disable the 'Show friendly HTTP errors' setting in IE!
Ferdinand Beyer
the code is already hosted on a shared hosting space. can i access the server log?
War Coder
A: 

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;
Gumbo