Have an account? Sign in
Login  Register  Facebook
Force Download Any file using PHP
Hello all, i need a working and error free script to force file downloads using PHP. plz help
I'm using a script from webinfopedia, its demo works fine but for my website is_readable($file) always returns FALSE but everything looks fine. if i skip this check, the file is downloaded but of 0 Bytes...
Started: February 2, 2012 Latest Activity: February 2, 2012 php filedownload
2 Answers
Thanks a ton for ur quick reply.. i'm going to use it now...

Posted: hassan.qazi
In: February 2, 2012

<?php

    $filename = $_GET['filename'];
    
    $download_path = "";
    
    if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
    $file = str_replace("..", "", $filename);
    
    if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
    
    $file = "$download_path$file";
    
    if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
    
    $type = filetype($file);
    
    $today = date("F j, Y, g:i a");
    $time = time();
    
    // Send file headers
    header("Content-type: $type");
    header("Content-Disposition: attachment;filename=$filename");
    header("Content-Transfer-Encoding: binary");
    header('Pragma: no-cache');
    header('Expires: 0');
    // Send the file contents.
    set_time_limit(0);
    readfile($file);

?>
and please give the folder 0777 chmode

Posted: MacOS
In: February 2, 2012

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer