473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How we can serve pdf using range header in php?

Hello,
Can anyone provide any Pdf byte serving example because people are talking about that but no one is providing solution ,I had tried with range header which I had previously used for video streaming but it is not working.

Can anyone please help me about "How we can serve pdf" using range header?
Jan 4 '18 #1
1 2242
Luuk
1,047 Expert 1GB
Adapt as needed the following:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file = "test.pdf";
  3. $fp = @fopen($file, 'rb');
  4. $size   = filesize($file); // File size
  5. $length = $size;           // Content length
  6. $start  = 0;               // Start byte
  7. $end    = $size - 1;       // End byte
  8.  
  9. header('Content-type: application/pdf');
  10. header("Accept-Ranges: bytes");
  11.  
  12. if (isset($_SERVER['HTTP_RANGE'])) {
  13.     $c_start = $start;
  14.     $c_end   = $end;
  15.  
  16.     list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  17.     if (strpos($range, ',') !== false) {
  18.         header('HTTP/1.1 416 Requested Range Not Satisfiable');
  19.         header("Content-Range: bytes $start-$end/$size");
  20.         exit;
  21.     }
  22.     if ($range == '-') {
  23.         $c_start = $size - substr($range, 1);
  24.     }else{
  25.         $range  = explode('-', $range);
  26.         $c_start = $range[0];
  27.         $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
  28.     }
  29.     $c_end = ($c_end > $end) ? $end : $c_end;
  30.     if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
  31.         header('HTTP/1.1 416 Requested Range Not Satisfiable');
  32.         header("Content-Range: bytes $start-$end/$size");
  33.         exit;
  34.     }
  35.     $start  = $c_start;
  36.     $end    = $c_end;
  37.     $length = $end - $start + 1;
  38.     fseek($fp, $start);
  39.     header('HTTP/1.1 206 Partial Content');
  40. }
  41.  
  42. header("Content-Range: bytes $start-$end/$size");
  43. header("Content-Length: ".$length);
  44.  
  45.  
  46. $buffer = 1024 * 8;
  47. while(!feof($fp) && ($p = ftell($fp)) <= $end) {
  48.  
  49.     if ($p + $buffer > $end) {
  50.         $buffer = $end - $p + 1;
  51.     }
  52.     set_time_limit(0);
  53.     echo fread($fp, $buffer);
  54.     flush();
  55. }
  56.  
  57. fclose($fp);
  58.  
Jan 6 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Newsgroup - Ann | last post by:
I have a library function like GetOptions( ..., struct net_arch_t netArch, ....) and put the declaration into a regular header file like getopt.h. But this function declaration also needs the...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
23
by: lwoods | last post by:
I am trying to pass some info to another page on my site. I set "session_start()" in page 1, assign a session variable to a value, then execute a "header('Location: ....')." But on the target...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
2
by: sundar22in | last post by:
Hi, I want to access a part of file in PHP(Instead of whole file). I understood HTTP Range header is the solution. How do we implement HTTP Range header in PHP??? Iam using fopen() method to...
0
by: Thanuja | last post by:
Hi please anyone help me. I am using wince API for sending request to server with range header field. I am giving range field as "Range:bytes=0-500"; The problem is request is going to server...
0
by: narf | last post by:
I'm running Apache2.0 on Debian Sarge. My goal is to refuse requests that are using the Range: header, for example when the client is trying to get a partial file. I can't find any information on...
10
by: jessica87 | last post by:
hi there, i m using this coding to retrieve the file from database so that my user can download the file... <?php if (!isset($_GET)) die('Invalid Parameter'); include...
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.