How to Read File into String in PHP

Read content of a file to string (in this example, we echo it):

// Get text from file
function get_text($text_filename) {
 if ($text_filename != "") {
  $text = "";
  $text_file = fopen($text_filename, "r");
  while (!feof($text_file)) {
      $text = $text . fgets($text_file, 4096);
  }
  fclose($text_file);
  if ( $text != "" ) {
    echo $text;
  }
 }
}

Eric Zhiqiang Ma

Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *