Download handler

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi
I've developed a download handler, it works perfectly in all cases but
one: When the file is bigger than the memory_limit from php, the
apache webserver falls down.

This is the code:
************************************************
$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 help will be appreciated!
Andres


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Everything you need is one click away.  Make Yahoo! your home page now.
http://us.click.yahoo.com/AHchtC/4FxNAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux