Search code examples
phphtmlweb-servicesscriptingserver-side

PHP - Creating a download link in HTML to all the list items fetched from a local folder


Lets say I have following script in PHP to create list of all the files from directory path /Users/abc/bde/fgh. Now I want to make them downloadable links for the same files, how can I achieve that?

$path = "/Users/abc/bde/fgh"; 

// Open the folder 
$dir_handle = @opendir($path) or die("Unable to open $path"); 

// Loop through the files 
while ($file = readdir($dir_handle)) { 

if($file == "." || $file == ".." || $file == "index.php" ) 
    continue; 
    echo "<a href=\"$file\">$file</a><br />";   
  } 

// Close        
closedir($dir_handle); 

Thanks in advance.


Solution

  • What you are looking for is probably a way to force a download for any file type right?

    Take a look at this code, you may want to add more mime types depending on what types of files you are having people download.

    This code was copied from: http://davidwalsh.name/php-force-download

    // http://davidwalsh.name/php-force-download
    // grab the requested file's name
    $file_name = $_GET['file'];
    
    // make sure it's a file before doing anything!
    if(is_file($file_name)) {
    
        /*
            Do any processing you'd like here:
            1.  Increment a counter
            2.  Do something with the DB
            3.  Check user permissions
            4.  Anything you want!
        */
    
        // required for IE
        if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
    
        // get the file mime type using the file extension
        switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
            case 'pdf': $mime = 'application/pdf'; break;
            case 'zip': $mime = 'application/zip'; break;
            case 'jpeg':
            case 'jpg': $mime = 'image/jpg'; break;
            default: $mime = 'application/force-download';
        }
        header('Pragma: public');   // required
        header('Expires: 0');       // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($file_name));    // provide file size
        header('Connection: close');
        readfile($file_name);       // push it out
        exit();
    
    }
    

    You just need to create a new php page (or same one) when they click a download link it goes to the new page (or same) with the file name parameter "file={filename}". For security do not include the file path. There are security issues with this method but it might not matter for you, all depends on your situation and what is being downloaded and if it is public data or not?