PHP read whole file into array or string
May 18, 2011 20:09:05 Last update: May 18, 2011 20:09:05
The PHP function file_get_contents reads the entire contents of a file into a string, while the function file reads a file into an array.
file_get_contents('filename') is equivalent to implode('', file('filename')):
<?php // reads entire file into array, one element for each line, with // newline still attached! // The return value if FALSE upon failure. $f = file("http://www.google.com/intl/en/contact/index.html"); // read entire file into a string. $s = file_get_contents("http://www.google.com/intl/en/contact/index.html"); echo "implode('', file('...')) is equivalent to file_get_contents('...'): ", (implode('', $f) === $s), "\n"; ?>