Freelancer's Playground! New knowledges every now and then

10Sep/072

File Downloader

Berikut ini adalah trik untuk membuat file downloader dimana file tersebut berada diluar dari web directory. Biasanya ditujukan untuk situs file download dimana akses ke file tersebut membutuhkan otorisasi login.

<?php
    function downloadFile($filepath){
        header('Content-Type: application/octet-stream');
        header('Content-Length: ' . filesize($filepath));
        header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
        @readfile($filepath);
    }

    function downloadFile2($filepath, $filename){
        header('Content-Type: application/octet-stream');
        header('Content-Length: ' . filesize($filepath));
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        @readfile($filepath);
    }

    $fpath = '/home/user/webuser/non_web_accessible_dir/file.pdf';
    $fname = 'file.pdf';

    downloadFile($fpath);
    downloadFile2($fpath, $fname);
?>

Diatas terdapat 2 function yaitu downloadFile dan downloadFile2. pada downloadFile, yang dibutuhkan hanya letak file tersebut di server. Sedangkan downloadFile2 digunakan bila kita ingin memberikan nama file yang berbeda dari file asli yang sedang didownload. Misalnya anda ingin membuat agar masing2 user mendownload file dimana pada file tersebut ada nama mereka. Contoh product_abc.zip ingin anda ubah menjadi product_abs_for_ariefbayu.zip.

  • http://bayu.freelancer.web.id/ Arief Bayu Purwanto

    mumpung baru muncul.
    eh…eh… ternyata bisa dibuat lebih simple, seperti ini:

    &lt;?php
        function downloadFile($filepath, $filename = ''){
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . filesize($filepath));
            header('Content-Disposition: attachment; filename=&quot;'.(trim($filename) == ''? basename($filepath) : $filename).'&quot;');
            @readfile($filepath);
        }
    ?&gt;
    

    trus cara pakenya:

    &lt;?php
        $fpath = '/home/user/webuser/non_web_accessible_dir/file.pdf';
    
        //downloadFile($fpath);
        //     atau
        downloadFile($fpath, 'nama_file_yang_baru.pdf');
    ?&gt;
    

    hihihihi, last minute hack :D

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    mumpung baru muncul.
    eh…eh… ternyata bisa dibuat lebih simple, seperti ini:

    <?php
        function downloadFile($filepath, $filename = ''){
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . filesize($filepath));
            header('Content-Disposition: attachment; filename="'.(trim($filename) == ''? basename($filepath) : $filename).'"');
            @readfile($filepath);
        }
    ?>
    

    trus cara pakenya:

    <?php
        $fpath = '/home/user/webuser/non_web_accessible_dir/file.pdf';
    
        //downloadFile($fpath);
        //     atau
        downloadFile($fpath, 'nama_file_yang_baru.pdf');
    ?>
    

    hihihihi, last minute hack :D