forcefully download any file php 2 comments



<pre class="brush: php; title: ; notranslate" title="">
////save this file abc.php
/////on any ahref
<?php
///USAGE SAVE IT AS SOME abc.php----->on ahref of any link pass --------abc.php?vi=pathname
//eg:abc.php?vi=uploads/prags.pdf

if(isset($_GET['vi']))
{
    //$tmp_path = "pdf/";
    $path_file =$_GET['vi'];
    //$tmp_file = $tmp_path.$tmp_file;

    if($path_file <> "")
    {
        $fileName = $path_file;
        header("Content-type: application/force-download");
        header("Content-Disposition: attachment; filename=".$fileName);
        header("Pragma: no-cache");
        header("Expires:0");
        readfile($fileName);
    }
}
?>

////////////////////////////////////////////////
<?php
$File = 'bpo_productlinks.csv';
header ("Content-Type: application/download");
header ("Content-Disposition: attachment; filename=$File");
header("Content-Length: " . filesize($File));
$fp = fopen($File, "r");
fpassthru($fp);
die;
?>
</pre>

Other very Quick Fix…

Just add..below lines in your .htaccess file..
If you want to download and PDF file.
You just need to give full path to your anchor tag

<a href="http://yoursite.com/uploads/pk.pdf">File Name</a>
AddType application/octet-stream .pdf

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools


2 thoughts on “forcefully download any file php