When we want to read the full content of a file we use file_get_contents() function. This function accepts the file name as the parameter and extract the entire content of the file as a string.
See this example. We are extracting the full content of the file a.txt in a variable $content and display.
$content = file_get_contents("a.txt");
echo $content;
When we want to read a specific bytes of file content then we are using fread() function.
In this example, we are extracting the first 10 bytes of data from file a.txt.
$file = fopen("a.txt", "r");
$content = fread($file, "10");
fclose($file);
echo $content;
Conclusion, file_get_contents() extract the full file content, whereas fread() extract the partial content of a file.
Тэги:
#php_file_get_contents_vs_fread #php_file_get_contents_vs_fread_with_example #php_file_get_contents_vs._fread_explained