Example of file() function in PHP. The file() function in PHP is used to read the whole file into an array.
Example of file() function in PHP. The file() function in PHP is used to read the whole file into an array.In this php tutorial section we will be learning the file() function of PHP. It is used to read the full content of file into an array.
Syntax of file() function in PHP
array file ( filename, [,flags [, context]] )
It returns the whole file into an array after reading.
It takes the file name as parameter.
To show the contents of the file no need of loop
flags have constants values as
FILE_BINARY(default),FILE_USE_INCLUDE_PATH,FILE_IGNORE_NEW_LINES,FILE_SKIP_EMPTY_LINES,FILE_TEXT,
stream_context_create() function creates the context
Code for file() in PHP
<?php
$ar1=file("c:/rose1/ram.txt");
for($x=0;$x<count($ar1);$x++)
echo "<br>".$ar1[$x];
?>
Code
<?php
$ar2=file("c:/rose1/ram.txt");
foreach ($ar2 as $key=>$val)
echo "<br>".$val;
?>
Output
welcome to roseindia
welcome to roseindia
welcome to roseindia
welcome to roseindia
welcome to roseindia
welcome to roseindia