Forum Moderators: coopster
Everything works fine, but when the file is big (lets say, 4 mb), it takes a LOT of time even before starting the download process (about five seconds). I've only made tests in the local server, and i think it should be faster.
I even made a test with a 50mb file and the apache server went down, i had to restart it.
# gen_ext retrieves the file extension
# $file is the complete /path1/path2/file.ext# Content-Type
switch(gen_ext($file)) {
case 'exe': $ctype='application/octet-stream'; break;
case 'zip': $ctype='application/zip'; break;
case 'pdf': $ctype='application/pdf'; break;
case 'doc': $ctype='application/msword'; break;
case 'xls': $ctype='application/vnd.ms-excel'; break;
case 'ppt': $ctype='application/vnd.ms-powerpoint'; break;
case 'gif': $ctype='image/gif'; break;
case 'png': $ctype='image/png'; break;
case 'jpe': case 'jpeg': case 'jpg': $ctype='image/jpg'; break;
default: $ctype='application/force-download';
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$ctype);
header('Content-Disposition: attachment; filename='.basename($file).'');
header('Content-Transfer-Encoding: Binary');
header('Content-Length: '.filesize($file));
readfile($file);
Any ideas?
$file = '/some_path1/some_path2/file.ext';# Content-Type
switch(gen_ext(basename($file))) {
case 'exe': $ctype='application/octet-stream'; break;
case 'zip': $ctype='application/zip'; break;
case 'doc': $ctype='application/msword'; break;
case 'pdf': $ctype='application/pdf'; break;
case 'ppt': $ctype='application/vnd.ms-powerpoint'; break;
case 'xls': $ctype='application/vnd.ms-excel'; break;
case 'gif': $ctype='image/gif'; break;
case 'jpe': case 'jpeg': case 'jpg': $ctype='image/jpg'; break;
case 'png': $ctype='image/png'; break;
case 'mp3': $ctype='audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3'; break;
case 'wma': $ctype='audio/x-ms-wma'; break;
case 'wmv': $ctype='video/x-ms-wmv'; break;
default: $ctype='application/force-download';
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$ctype);
header('Content-Disposition: attachment; filename='.basename($file).'');
header('Content-Transfer-Encoding: Binary');
header('Content-Length: '.filesize($file));
# Defines memory limit. If there's no access to funcion, we use default value (2 MB)
$mem = (function_exists('memory_get_usage'))? memory_get_usage() : (2*(1024*1024));
if(filesize($file)<=$mem) {
# If filesize is less than memory limit, the file gets directly downloaded
readfile($file);
} else {
# If filesize is bigger than memory limit, the file gets downloaded in chunks
if(($f = fopen($file,'rb')) === false) die();
while (!feof($f)) {
echo fread($f,(1*(1024*1024)));
flush();
@ob_flush();
}
fclose($f);
}
ANY IDeAS?
but still I have the timeout problem for big files.
What is the error message? Does set_time_limit [php.net] help?
$mem = ini_get('memory_limit');
$mem = substr($mem,0,strpos($mem,'M'));
if(!is_numeric($mem)) {
$mem = 2*(1024*1024);
}
Still the timeout function wouldnt help, because i still need the file to get downloaded.
Ive also changed the
echo fread($f,(1*(1024*1024)));
with:
echo fread($f,1024*4);
But other problems appear to happen. For example, a video gets downloaded but the file consistency is lost and the downloaded file cant be played.
By the way, the script functions alright now. The only problem is that it takes to long to download the files (tested on local server).
For instance, cv.doc (76.5 KB) takes 9 seconds to get downloaded. The transfer rate is 8.50 KB/sec (on a local host!)
Any ideas on that? Here's the code:
# Setting para que Content-disposition funcione en IE
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');# Content-Type
switch(gen_ext(basename($file))) {
case 'exe': $ctype='application/octet-stream'; break;
case 'zip': $ctype='application/zip'; break;
case 'doc': $ctype='application/msword'; break;
case 'pdf': $ctype='application/pdf'; break;
case 'ppt': $ctype='application/vnd.ms-powerpoint'; break;
case 'xls': $ctype='application/vnd.ms-excel'; break;
case 'gif': $ctype='image/gif'; break;
case 'jpe': case 'jpeg': case 'jpg': $ctype='image/jpg'; break;
case 'png': $ctype='image/png'; break;
case 'mp3': $ctype='audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3'; break;
case 'wma': $ctype='audio/x-ms-wma'; break;
case 'wmv': $ctype='video/x-ms-wmv'; break;
case 'php': die(); break;
default: $ctype='application/force-download';
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$ctype);
header('Content-Length: '.filesize($file));
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: Binary');
@readfile($file);
What could be the slow transfer rate problem?