Sorry, this page is only available in one language

Dynamic download links

Suggested dropletname: dyndwn

Published by: Dev4me

Create a download link that is unique for the current session only. The direct location of the download will not be visible.


$parm = "dwn";
$path = WB_PATH.MEDIA_DIRECTORY."/files/";
$ip = preg_replace(['/.d*$/','/[da-f]*:[da-f]*$/'],['.XXX','XXXX:XXXX'],$_SERVER['REMOTE_ADDR']);
if(isset($_GET[$parm])) {
  $id = $_GET[$parm];
  if(isset($_SESSION['dyndwn'][$id])) {
    $download =  $_SESSION['dyndwn'][$id];
    $contenttype = mime_content_type ($download);
    $filename = str_replace($path,'',$download);
    error_log("$ip - Download: $filename");
    header("Content-Description: File Transfer");
    header("Content-type: $contenttype");
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header("Content-Length: " . filesize($download));
    header('Pragma: public');
    header("Expires: 0");
    readfile($download );
} else {
  if(!isset($file)) return true;   // no filename
  if(!isset($title)) $title = $file;   // no link text
  if(!file_exists($path.$file)) return true;  // file does not exist
  $guid = bin2hex(random_bytes(8));
  $_SESSION['dyndwn'][$guid] = $path.$file;
  return '<a target="dwn'.$guid.'" href="?'.$parm.'='.$guid.'">'.$title.'</a><iframe style="height:0;width:0;display:none;" name="dwn'.$guid.'"></iframe>';
return true;

Note: change the $path variable in line 2 to the file location in your media folder.


Name the droplet "dyndwn"

minimal: [[dyndwn?file=myfile.pdf]]

or: [[dyndwn?file=myfile.pdf&title=download the file now]]

The download is "recorded" in your error logfile with the anonimized IP address of the downloader. If you do not want that, remove line 10 (error_log(..... etc )

| Overview | Next droplet »