Interview Questions

How To Read the Entire File into a Single String?

PHP Interview Questions and Answers


(Continued from previous question...)

How To Read the Entire File into a Single String?

If you have a file, and you want to read the entire file into a single string, you can use the file_get_contents() function. It opens the specified file, reads all characters in the file, and returns them in a single string. Here is a PHP script example on how to file_get_contents():

<?php
$file = file_get_contents("/windows/system32/drivers/etc/services");
print("Size of the file: ".strlen($file)."\n");
?>

This script will print:

Size of the file: 7116

Other Interview Questions