Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Web Design (WordPress, Joomla, etc.) Download function PHP

  • Download function PHP

    Posted by Rachel Broomfield on December 11, 2010 at 9:28 pm

    Hello…
    If anyone could help, I would greatly appreciate it. I am trying to create a “download” function on my site. The visitor would be able to click on the “download” key and print the form.

    This is what I have:

    <?php

    // place this code inside a php file and call it f.e. “download.php”
    $path = $_SERVER[‘DOCUMENT_ROOT’].”/path2file/”; // change the path to fit your websites document structure
    $fullPath = $path.$_GET[‘download_file’];

    if ($fd = fopen ($fullPath, “r”)) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts[“extension”]);
    switch ($ext) {
    case “pdf”:
    header(“Content-type: application/pdf”); // add here more headers for diff. extensions
    header(“Content-Disposition: attachment; filename=\””.$path_parts[“basename”].”\””); // use ‘attachment’ to force a download
    break;
    default;
    header(“Content-type: application/octet-stream”);
    header(“Content-Disposition: filename=\””.$path_parts[“basename”].”\””);
    }
    header(“Content-length: $fsize”);
    header(“Cache-control: private”); //use this to open files directly
    while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
    }
    }
    fclose ($fd);
    exit;
    // example: place this kind of link into the document where the file download is offered:
    // Download here
    ?>

    Mike Smith replied 15 years, 8 months ago 2 Members · 1 Reply
  • 1 Reply
  • Mike Smith

    December 12, 2010 at 10:32 am

    You know that you could simply use a link to your document, unless you are dynamically generating different versions of the file for each person who will download it ?

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy