simple dowloader script in php ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

simple dowloader script in php ??

Post by PHPycho »

Hello forums!!
I havent use the php downloader script(ie using headers).
Can anybody suggest me how to write downloading script in php?
Thanks in advance to all of you
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

We've had a lot of threads covering this subject. Please search for "force download" and/or read the manual on header().
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

I found this one,
I dont know how effective is this?
any comments on this code:

Code: Select all

<?php
    
    function getmimetype ($filename) {
        static $mimes = array(
            '\.jpg$|\.jpeg$'  => 'image/jpeg',
            '\.gif$'          => 'image/gif',
            '\.png$'          => 'image/png',
            '\.html$|\.html$' => 'text/html',
            '\.txt$|\.asc$'   => 'text/plain',
            '\.xml$|\.xsl$'   => 'application/xml',
            '\.pdf$'          => 'application/pdf'
        );
    
        foreach ($mimes as $regex => $mime) {
            if (eregi($regex, $filename)) return $mime;
        }
        return 'text/plain';
    }
    $file = $_GET["file"];
    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Type: ' . getmimetype($file));
    header('Content-Disposition: attachment; filename=' . basename($file) . ';');
    header('Content-Length: ' . filesize($file));
 
    readfile($file);
?>
Post Reply