3

We need your help with one PHP project client restriction.

He wants full download security video from server, he wants video storage be out of public directory, and video streaming must be in HTML5, without Flash. I have one solution, load video in php buffer and response it changing header info, but video size, more or less 300MB is impossible to load in PHP buffer...

Any good idea to do it?

Dani
  • 4,001
  • 7
  • 36
  • 60
  • 1
    are we talking about streaming or download? – Gianluca Ghettini Nov 12 '12 at 21:25
  • 3
    so how much of the clients fee are you paying us to do this for you? –  Nov 12 '12 at 21:26
  • Read the file in chunks and send those to the client as they come in. Don't read the whole file at once. – Brad Nov 12 '12 at 21:28
  • Good call, @G_G . I too had read 'load video in PHP buffer' and had translated with 'download'. Streaming is a bit more complicated as it requires parsing (and generating) the correct headers, but it's doable without resorting to freakishly large buffers. – LSerni Nov 12 '12 at 21:35

1 Answers1

4

You don't need to load anything into a buffer. Just send the headers, then follow them up with the file read using readfile(). The file is in a directory whose .htaccess prohibits direct access (the download script, of course, is not), and Bob's your uncle.

Since you talked about loading the file into the buffer, mind, this is not "streaming". What most Flash streamers do is actually issue Byte-Range requests. In those cases too, you can recognize the situation with if (isset($_SERVER['HTTP_RANGE'])), parse the range, and do the rest with fseek's and fread's.

LSerni
  • 55,617
  • 10
  • 65
  • 107
  • **readfile() will not present any memory issues, even when sending large files**, on its own. If you encounter an out of memory error ensure that output buffering is off with ob_get_level(). This solve my problem... true? – Dani Nov 12 '12 at 21:39
  • Exactly -- for the *download*. For the *streaming* you won't be using `readfile` anyway. – LSerni Nov 13 '12 at 07:45