Support » Plugins » Hacks » Protected Downloads via PHP

  • having a problem with large downloads from a htaccess protected directory. Currently I’m using this:
    version 1:

    //force download - should bring up save box, but it doesn't!
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: application/force-download");
    // it even allows spaces in filenames
    header('Content-Disposition: attachment; filename="'.$item.'"');
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($dload));

    But it fails for large downloads, so I have tried replacing readfile with: eshop_readfile($dload);
    Which leads to:

    if (!function_exists('eshop_readfile')){
      // Read a file and display its content chunk by chunk
      function eshop_readfile($filename, $retbytes = TRUE) {
        $buffer = '';
        $cnt =0;
        $chunksize=1024*1024;// Size (in bytes) of tiles chunk
        $handle = fopen($filename, 'rb');
        if ($handle === false) {
          return false;
        while (!feof($handle)) {
          $buffer = fread($handle, $chunksize);
          echo $buffer;
          if ($retbytes) {
            $cnt += strlen($buffer);
        $status = fclose($handle);
        if ($retbytes && $status) {
          return $cnt; // return num. bytes delivered like readfile() does.
        return $status;

    But apparently that doesn’t work either.

    So I’ll assume I’m doing something wrong, and ask for help!
    Remember: download directory is htaccess protected.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Protected Downloads via PHP’ is closed to new replies.