16

I am trying to read a large file (10M) using php file_get_contents

$file = 'http://www.remoteserver.com/test.txt';
$data = file_get_contents( $file );
var_dump ( $data );

It dumps back

string(32720)

and then the output with only showing part of the file. Is there a limit somewhere of file_get_contents? I tried doing ini_set('memory_limit', '512M'), but that did not work.

EDIT: ** forgot to mention ** it's a remote file.

PROBLEM RESOLVED:: Out of HDD space. Fixed that and now everything works.

Scott Foster
  • 465
  • 3
  • 9
  • 17
  • There shouldn't be such a limit. Does it show the start or the end of the file? Are you sure it reads the correct file? What does it dump when you shorten the file somewhat? – AndreKR Sep 27 '12 at 19:37
  • Have it dump `filesize( $file )` before loading and `strlen( $data )` after loading. I'm kinda curious what it comes up with. – Mr. Llama Sep 27 '12 at 19:38
  • Have you attempted to do an fread() on the file? If so, are you encountering the same issue? – Tim Ferrell Sep 27 '12 at 19:39
  • Try downloading the file with a browser, store it locally and then use file_get_contents() on that to narrow the problem down. The only limit here should be local memory, but in that case you'd get an error message from PHP (if errors are set for display). – Wolfgang Stengel Sep 27 '12 at 19:45
  • 1
    Interesting... 32720 is very close to 32767, the max 4-bit signed integer. Whether it's just a coincidence, I'm not sure... – Aleks G Sep 27 '12 at 19:45
  • @TFerrell - gives me error since it's a remote file. – Scott Foster Sep 27 '12 at 19:52
  • @WolfgangStengel it worked that way. – Scott Foster Sep 27 '12 at 19:53
  • Then the remote bit is the problem apparently. Maybe var_dump($http_response_header) after the call reveales something? – Wolfgang Stengel Sep 27 '12 at 20:01
  • @WolfgangStengel checking with rackspace. Other files work just fine, it's just this large file that is the problem. – Scott Foster Sep 27 '12 at 20:20
  • @Scott Remote file? How are you accessing it? I've seen Windows do some strange things. – Tim Ferrell Sep 27 '12 at 23:19
  • PHP can read much larger files than 10MB... – Ian Sep 30 '12 at 05:08
  • I think this wins the 'too localized of the month' award :) I hate to ponder how long it took you to realize what was actually going on there - we all have days like that :) – Tim Post Sep 30 '12 at 07:50
  • Just FYI: (That was my problem) file_get_contents() is also limited by the `memory_limit`! – CodeBrauer Aug 19 '14 at 10:12

1 Answers1

-1

Assuming the contents of the file you want to load are logically separated by line breaks (eg: not a binary file), then you might be better off reading line by line.

$fp = fopen($path_to_file, "r");  
$fileLines = array();
while (!feof($fp)){
  array_push(fgets($fp),$fileContents);
} 
fclose($$fp);

You could always implode() (with your choice of line break character) the array back to a single string if you really need the file in one "chunk".

Reference -

Lix
  • 47,311
  • 12
  • 103
  • 131
  • I suspect that with such large files they are binary, so `fgets` may not be a good idea. – Aleks G Sep 27 '12 at 19:39
  • @ale - good point. I've clarified my assumption regarding the file in question. – Lix Sep 27 '12 at 19:42
  • @ale - (just noticed) in the code sample provided by the OP, a `.txt` extension is provided. So this might be relevant after all. – Lix Sep 27 '12 at 19:46
  • good point, I missed the extension – Aleks G Sep 27 '12 at 19:49
  • 1
    `fgets()` will also randomly truncate lines larger than 16k (as will `stream_get_line()` (more or less, depending on how you set chunk size), it's better to just use `fread()` and handle EOL / EOF yourself if you expect very long lines. That actually just bit me in the rear in a rather nasty way a week ago. However, I do believe fgets() is (5.3+) binary safe, so long as you look out for very long lines. – Tim Post Sep 30 '12 at 07:45
  • your code doesn't work bro, it's full of typos – mattspain Dec 26 '19 at 12:41
  • @mattspain - this was written over 7 years ago hehe.. I don't think I've touched PHP in 7 years either :P – Lix Dec 26 '19 at 12:43
  • @mattspain - You're very much welcome to make some edits to the answer and point out the issues - thats what this site is all about! :) – Lix Dec 26 '19 at 12:44
  • the easiest and quickest way would be to remove the answer since OP solved his problem :) – mattspain Dec 26 '19 at 12:45