Files
freeCodeCamp/guide/english/php/functions/files/file-reading/index.md
RichardLimSpring f319a46b14 Added 2 File reading functions (#30120)
* Added 2 reading functions

Added :  fopen for reading, fread function and file_get_contents function.

* Update index.md

* Update index.md
2019-03-28 15:19:22 -07:00

1.3 KiB

title
title
File Reading

File Reading

PHP prepared File Reading functions to ease user to only retrieve information outside PHP.

fopen("fileName.txt", "r")

Before we read a file, we need to prepare the file in PHP using fopen function reading mode("r"). The $fileHandler will be the file handler variable in the file reading operation.

<?php

$fileHandler = fopen("fileName.txt", "r");

?>

fread()

After the desired file to be read has been prepared for reading, user can proceed to fread function to read contents of the file.

<?php

$content = fread($fileHandler, filesize("fileName.txt"));

?>

Content in the file "file.txt" will be stored in the variable $content.

file_get_contents()

This file reading function is rather easy. This function doesn't need the file to be prepare by fopen().

<?php

$content = file_get_contents("fileName.txt");

?>

More Information: