Share on Google+Share on Google+

The file function example in PHP, file php, file in php

Advertisement
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

 

Advertisements

Posted on: September 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.