tags:

views:

223

answers:

4

is file_get_contents() enough for downloading remote movie files located on a server ?

i just think that perhaps storing large movie files to string is harmful ? according to the php docs.

OR do i need to use cURL ? I dont know cURL.

UPDATE: these are big movie files. around 200MB each.

+2  A: 

As @mopoke suggested it could depend on the size of the file. For a small movie it may suffice. In general I think cURL would be a better fit though. You have much more flexibility with it than with file_get_contents().

Darrell Brogdon
+1  A: 

For the best performance you may find it makes sense to just use a standard unix util like WGET. You should be able to call it with system("wget ...") or exec() http://www.php.net/manual/en/function.system.php

MindStalker
cURL has the advantage of not spawning a separate process, which, for large numbers of requests, is a huge advantage.
Frank Farmer
+3  A: 

file_get_contents() is a problem because it's going to load the entire file into memory in one go. If you have enough memory to support the operation (taking into account that if this is a web server, you may have multiple hits that generate this behavior simultaneously, and therefore each need that much memory), then file_get_contents() should be fine. However, it's not the right way to do it - you should use a library specifically intended for these sort of operations. As mentioned by others, cURL will do the trick, or wget. You might also have good luck using fopen('http://someurl', 'r') and reading blocks from the file and then dumping them straight to a local file that's been opened for write privileges.

Dathan
Read the docs. `file_get_contents()` also supports reading files in chunks.
Alix Axel
Where does it say that?
George Edison
file_get_contents has paramaters for "offset" and "maxlen" as of PHP 5.1. I know -- it's news to me too. A problem, however: making multiple calls to file_get_contents() inevitably closes and reopens the "file" in question -- I'd assume downloading the last byte of a 200 megabyte file via HTTP with file_get_contents() may very well download all 200 MB. It'd be worth testing, although there's no good reason not to just use fopen() instead.
Frank Farmer
@Alix thanks - I didn't realize that, as I've only ever used the overload that returns the entire file in one call. Do you know anything about network performance (e.g., number of requests generated) of using file_get_contents in chunks versus reading from a remote file stream?
Dathan
cURL is much better.
George Edison
A: 

you can read a few bytes at a time using fread().

$src="http://somewhere/test.avi";
$dst="test.avi";
$f = fopen($src, 'rb');
$o = fopen($dst, 'wb');
while (!feof($f)) {
     if (fwrite($o, fread($f, 2048)) === FALSE) {
        return 1;
     }
}
fclose($f);
fclose($o);
ghostdog74
I use this on my site, but beware - remember script max execution time.
George Edison