WordPress.org

Ready to get started?Download WordPress

Forums

Protected Downloads via PHP (2 posts)

  1. elfin
    Moderator
    Posted 3 years ago #

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

    set_time_limit(0);
    $item=$chkrow->files;
    //force download - should bring up save box, but it doesn't!
    $dload=$dir_upload.$item;
    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));
    readfile("$dload");
    exit();

    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;
          ob_flush();
          flush();
          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.

  2. elfin
    Moderator
    Posted 3 years ago #

    erm, anyone?

Topic Closed

This topic has been closed to new replies.

About this Topic