Read a file into a string using PHP

An entire file can be read into a single string using the file_get_contents() php function. The file function reads all the lines of a file and returns them in a single string.

Example code on how to file_get_contents():

<?php 
$file = file_get_contents("/tmp/file.txt");
print("Size of the file: ".strlen($file)."\n");
?>

Output:
Size of the file: 7362

The $file string contains all the contents of the file and can be used for manipulation.

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker