File/Directory
How to create New file and open Existing file

The fopen() function is used to create new file and also open existing file.

Syntax:-
fopen("filename","mode");

Mode:-

Mode Description
r read only
w write only
a append
Some Usefull File Handling Function are:

fclose()
This function closes the opening file. Return type bool.
$file=fopen("myfile.txt","r");
fclose($file);

fwrite()
To write the content into the file. Return type int.
$file=fopen("myfile.txt","w");
fwrite($file,'my name is shishir');

fgets()
To read a single line from a file. Return type string.
$file=fopen("myfile.txt","r");
echo fgets($file)

fgetc()
To read a single character from a file. Return type string.
$file=fopen("myfile.txt","r");
echo fgetc($file);

feof()
This function check end-of-file. if you are at the end of file this function return true otherwise return false.Return type bool.
$file=fopen("myfile.txt","r");
while(!feof($file))
{
...... code here
}

file_exists()
This function is use to check whether a file or directory exists or not.This function return true if file exists otherwise false. Return type bool.
file_exists("file.txt");

file_size()
This function return the file size in bytes. Return type integer
filesize("file.txt");

Example 1:- PHP script to create new msword file in d: and also write content into the file.

<?php
$file = fopen("d:\welcome.doc", "w") ;
fwrite($file,'my name is shishir');
fclose($file);
?>

Note:- When we take (w) mode means we are create new file.

Example 2 :- PHP script to open msword file from d: in append mode and also add some content into the file.

<?php
$file = fopen("d:\welcome.doc", "a") ;
fwrite($file,'my name is shishir');
fclose($file);
?>

Note:- When we take (a) mode means we open existing file. if file does not exist then (a) mode create new file

Example 3 :- we are checking the MULTIMEDIA directory exist in d: or not.

<?php
if(file_exists("d:\MULTIMEDIA"))
echo "exists";
else
echo "Note exists";
?>

Output:-
exists

Example 4:- PHP script to open file and read it contain line by line

<?php
$file = fopen("welcome.txt", "r") ;
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>

Example 5:- PHP script to open file and read it contain character by character.

<?php
$file=fopen("welcome.txt","r");
while (!feof($file))
{
  echo fgetc($file);
}
fclose($file);
?>

Example 6:- PHP script to read whole file at once

<?php
if(file_exists("myfile.txt"))
{
$str=file_get_contents("myfile.txt");
$text=str_replace("\n","<br>",$str);
echo $text;
}
?>

Example 7:- PHP script to write whole file at once

<?php
$str_text="This\nis\ntext\nexample";
file_put_contents("myfile.txt",$str_text);
?>

Directory in PHP

There are some function to handle directory.

opendir():-
To open any directory.
readdir():-
To all file stored in directory.
closedir():-
To close any directory.

Example 8:- By this example we are open directory and print files name

<?php
$handle = opendir('upload');
if($handle)
{
while($file = readdir($handle))
{
if ($file != "." && $file != "..")
{
echo "$file\n"."<br>";
}
}
closedir($handle);
}
?>


Previous
Next