File Download By Php

Suppose you want to attach a file download code to a document link. Here is how you can do it.

Suppose you have the link in the page "index.php" then, write there the following:

<a href='/download.php?choice=docdwnld&file=download_file_name' target='_blank'>Download File Title</a>

Here :
"download_file_name" is the filename to download.
"Download File Title" is the title you want to show on the clickable link.

Now create a file called "download.php" in your root and add the following code:

<?php
$choice = $_REQUEST['choice'];
switch($choice)
{
    case 'docdwnld':
        $directory = $_SERVER['DOCUMENT_ROOT'] . '/download/folder/path/';
        $file = $_REQUEST['file'];
        $filename = $directory . $file;
        ob_clean();
        header("Cache-Control: no-store");
        header("Expires: 0");
        header("Content-Type: application/octet-stream");
        header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
        header("Content-Transfer-Encoding: binary");
        header('Content-Length: '. filesize($filename));
        readfile($filename); 
    break;
}
?>

Here:
"/download/folder/path/" is the path to the file you want to download. Note the trailing slashes.