Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 388633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:47:18+00:00 2026-05-12T15:47:18+00:00

I’m writing a PHP script that allows the user to download a file. Basically

  • 0

I’m writing a PHP script that allows the user to download a file. Basically the idea is to prevent the file being downloaded more than X times, since it is paid content, and the link should not be spread around.

Since the files will be pretty large, it should be good to implement resuming. I’ve read the standard, but it’s pretty long and allows for some flexibility. Since I need to get it done quickly, I’d prefer a stable, tested implementation of this feature.

Can anyone point me to such a a script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:47:19+00:00Added an answer on May 12, 2026 at 3:47 pm

    Seems that I found what I needed myself. So that other may benefit from this, here is the link: http://www.coneural.org/florian/papers/04_byteserving.php

    And just in case the original page stops to work (the script is pretty old already), here is a copy of it:

    <?php 
    /*
    
    The following byte serving code is (C) 2004 Razvan Florian. You may find the latest version at 
    http://www.coneural.org/florian/papers/04_byteserving.php
    
    */
    function set_range($range, $filesize, &$first, &$last){
      /*
      Sets the first and last bytes of a range, given a range expressed as a string 
      and the size of the file.
    
      If the end of the range is not specified, or the end of the range is greater 
      than the length of the file, $last is set as the end of the file.
    
      If the begining of the range is not specified, the meaning of the value after 
      the dash is "get the last n bytes of the file".
    
      If $first is greater than $last, the range is not satisfiable, and we should 
      return a response with a status of 416 (Requested range not satisfiable).
    
      Examples:
      $range='0-499', $filesize=1000 => $first=0, $last=499 .
      $range='500-', $filesize=1000 => $first=500, $last=999 .
      $range='500-1200', $filesize=1000 => $first=500, $last=999 .
      $range='-200', $filesize=1000 => $first=800, $last=999 .
    
      */
      $dash=strpos($range,'-');
      $first=trim(substr($range,0,$dash));
      $last=trim(substr($range,$dash+1));
      if ($first=='') {
        //suffix byte range: gets last n bytes
        $suffix=$last;
        $last=$filesize-1;
        $first=$filesize-$suffix;
        if($first<0) $first=0;
      } else {
        if ($last=='' || $last>$filesize-1) $last=$filesize-1;
      }
      if($first>$last){
        //unsatisfiable range
        header("Status: 416 Requested range not satisfiable");
        header("Content-Range: */$filesize");
        exit;
      }
    }
    
    function buffered_read($file, $bytes, $buffer_size=1024){
      /*
      Outputs up to $bytes from the file $file to standard output, $buffer_size bytes at a time.
      */
      $bytes_left=$bytes;
      while($bytes_left>0 && !feof($file)){
        if($bytes_left>$buffer_size)
          $bytes_to_read=$buffer_size;
        else
          $bytes_to_read=$bytes_left;
        $bytes_left-=$bytes_to_read;
        $contents=fread($file, $bytes_to_read);
        echo $contents;
        flush();
      }
    }
    
    function byteserve($filename){
      /*
      Byteserves the file $filename.  
    
      When there is a request for a single range, the content is transmitted 
      with a Content-Range header, and a Content-Length header showing the number 
      of bytes actually transferred.
    
      When there is a request for multiple ranges, these are transmitted as a 
      multipart message. The multipart media type used for this purpose is 
      "multipart/byteranges".
      */
    
      $filesize=filesize($filename);
      $file=fopen($filename,"rb");
    
      $ranges=NULL;
      if ($_SERVER['REQUEST_METHOD']=='GET' && isset($_SERVER['HTTP_RANGE']) && $range=stristr(trim($_SERVER['HTTP_RANGE']),'bytes=')){
        $range=substr($range,6);
        $boundary='g45d64df96bmdf4sdgh45hf5';//set a random boundary
        $ranges=explode(',',$range);
      }
    
      if($ranges && count($ranges)){
        header("HTTP/1.1 206 Partial content");
        header("Accept-Ranges: bytes");
        if(count($ranges)>1){
          /*
          More than one range is requested. 
          */
    
          //compute content length
          $content_length=0;
          foreach ($ranges as $range){
            set_range($range, $filesize, $first, $last);
            $content_length+=strlen("\r\n--$boundary\r\n");
            $content_length+=strlen("Content-type: application/pdf\r\n");
            $content_length+=strlen("Content-range: bytes $first-$last/$filesize\r\n\r\n");
            $content_length+=$last-$first+1;          
          }
          $content_length+=strlen("\r\n--$boundary--\r\n");
    
          //output headers
          header("Content-Length: $content_length");
          //see http://httpd.apache.org/docs/misc/known_client_problems.html for an discussion of x-byteranges vs. byteranges
          header("Content-Type: multipart/x-byteranges; boundary=$boundary");
    
          //output the content
          foreach ($ranges as $range){
            set_range($range, $filesize, $first, $last);
            echo "\r\n--$boundary\r\n";
            echo "Content-type: application/pdf\r\n";
            echo "Content-range: bytes $first-$last/$filesize\r\n\r\n";
            fseek($file,$first);
            buffered_read ($file, $last-$first+1);          
          }
          echo "\r\n--$boundary--\r\n";
        } else {
          /*
          A single range is requested.
          */
          $range=$ranges[0];
          set_range($range, $filesize, $first, $last);  
          header("Content-Length: ".($last-$first+1) );
          header("Content-Range: bytes $first-$last/$filesize");
          header("Content-Type: application/pdf");  
          fseek($file,$first);
          buffered_read($file, $last-$first+1);
        }
      } else{
        //no byteserving
        header("Accept-Ranges: bytes");
        header("Content-Length: $filesize");
        header("Content-Type: application/pdf");
        readfile($filename);
      }
      fclose($file);
    }
    
    function serve($filename, $download=0){
      //Just serves the file without byteserving
      //if $download=true, then the save file dialog appears
      $filesize=filesize($filename);
      header("Content-Length: $filesize");
      header("Content-Type: application/pdf");
      $filename_parts=pathinfo($filename);
      if($download) header('Content-disposition: attachment; filename='.$filename_parts['basename']);
      readfile($filename);
    }
    
    //unset magic quotes; otherwise, file contents will be modified
    set_magic_quotes_runtime(0);
    
    //do not send cache limiter header
    ini_set('session.cache_limiter','none');
    
    
    $filename='myfile.pdf'; //this is the PDF file that will be byteserved
    byteserve($filename); //byteserve it!
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have actually long pondered upon this scenario and have… May 12, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer You might try enabling handshaking, using the Handshake property of… May 12, 2026 at 6:12 pm
  • Editorial Team
    Editorial Team added an answer My recommendation would be to use SSL for the page… May 12, 2026 at 6:12 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.