> Online tutorial : read file in php
Showing posts with label read file in php. Show all posts
Showing posts with label read file in php. Show all posts

read directory php

read directory php
<?php
// assign folder into variable
$currdir='/var/www';
//open the directory for reading $dir=opendir($currdir);
//read a filename
while($file = readdir($dir)
{
// Display all file name in the specified folder
echo "$file<br/>";
}
closedir($dir);
}?>
Output:

index.html
est.html
first.php

Explanation
Now opendir simply opens a directory,replace folder with the name of the folder and if needed, the path too.The while loop then goes through file by file in the directory .the if statement exclude s showing two non-files.you can exclude any files you wish too.After a simpl e echo displaying the filename




writes file in php

writes file in php



In PHP we are using fwrite() function to retrieve data in file.we are using fwrite() function that will return how many character will placed in the file only it is display.



html>


<body>
<?php
$content="hello world";
$f=fopen('today.txt','w');
// today.txt must in read.php location
$fh=fwrite($f,$content);
// it is no of character in content
echo $fh;
fclose($f);
?>
</body>
</html>

Output:





read file in php

read from file in php
the fread() reads from an open file.The function will stop at the end of the file or when it reaches the specified length.whichever comes first.this function the read string or false on failure
syntax

frad(file,length)

Example
<html>

<body>
<?php
$f=fopen('today.txt','r');
// today.txt must in read.php location
$fh=fread($f,"6");
echo $fh;
fclose($f);
?>
</body>
</html>

Output