Unit-4
Working with Files and Directories:
Including Files with include() function:
- Including the content of a PHP file into another file reduces the
complexity of code by reducing the code.
- There are 2 ways to do it.
1) include() function
2) require() function
include() function:
- It includes the content of a PHP file into another file.
- If the file does not exist, Then it will show warning and continue the rest
of code in the program.
firstfile.php
<html>
<body>
<h2>This is the Content of firstfile.php</h2>
<?php
include("secondfile.php");
?>
<h2> This is text after include function </h2>
</body>
</html>
secondfile.php
<?php
echo "<h2>This is the content of secondfile.php </h2>";
echo "<h3> Hello, Good to see you </h3>";
?>
Output:
This is the Content of firstfile.php
This is the content of secondfile.php
Hello, Good to see you
This is text after include function
require() function:
- It includes the content of a PHP file into another file.
- If the file does not exist, Then it will show error and stops the program.
firstfile.php
<html>
<body>
<h2>This is the Content of firstfile.php</h2>
<?php
require("secondfile.php");
?>
<h2> This is text after include function </h2>
</body>
</html>
secondfile.php
<?php
echo "<h2>This is the content of secondfile.php </h2>";
echo "<h3> Hello, Good to see you </h3>";
?>
Validating files
- PHP provides many functions to help you to discover information about
files on your system.
- Some of the most useful functions are:
file_exists()
is_file()
is_readable()
is_writable()
is_executable()
filesize()
file_exists():
- It is used to check whether the file or directory is exist.
- It returns true if the file/directory is exist. Otherwise, it returns false.
<?php
$flg=file_exists("./test.php");
if($flg==true)
echo "File Exist";
else
echo "File does not exist";
clearstatcache();
?>
is_file():
- This function returns True, if the given file is exists and it is a regular file,
Otherwise it returns false
- It can not check a directory file or special file.
- Regular file,It may be readable file/Binary file/Image file/Compressed
file/symbolic link
- It is a case sensitive.
<?php
$flg=is_file("./test.php");
if($flg==true)
echo "Given file is a regular file";
else
echo "Given file is not regular file";
clearstatcache();
?>
is_readable():
- It checks whether file has readable permission. if yes, It returns true.
Otherwise, It returns false.
- It is a case-sensitive
<?php
$flg=is_readable("./bb.txt");
if($flg==true)
echo "File is readable";
else
echo "File is not readable";
clearstatcache();
?>
is_writable():
- It checks whether the file has writable permission or not. If yes, It
returns true. Otherwise, It returns false.
<?php
$flg=is_writable("./bb.txt");
if($flg==true)
echo "File is writable";
else
echo "File is not writable";
clearstatcache();
?>
is_executable():
- It checks whether the file is executable or not. If yes, It returns true.
Otherwise, It returns false.
- It is case sensitive
<?php
$flg=is_executable("./bb.txt");
if($flg==true)
echo "File is Executable";
else
echo "File is not Executable";
clearstatcache();
?>
filesize():
- This function returns the accurate size of the file in bytes.
- But, If the size of the file is greater than 100MB, Then the result may not
be accurate.
<?php
$file_size=filesize("./bb.txt");
echo $file_size;
clearstatcache();
?>
Creating and Deleting Files:
- In Unix operating System, We can create and delete files using touch()
and unlink() functions.
- If a file does not exist, you can create a file using touch() function.
- touch() function attempts to create an empty file with the given name.
- If the file already exists, its contents is not disturbed but, the
modification date is updated.
Ex:- touch(‘myfile.txt’);
- You can remove an existing file with the unlink() function.
Ex:- unlink(‘myfile.txt);
Opening a File for Reading, Writing or Appending:
- Before you work with a file, you must first open it for reading or writing
or for both tasks.
- PHP provides the fopen() function for opening a file and this function
requires a string that contains the file path followed by a string that the
mode in which the file is to be opened.
- The common modes are read (r) , write (w) and append (a).
- The fopen() function returns a file resource.
- To open a file for reading , you use the following syntax.
$fp = fopen(“filename”,”mode”)
$fp = fopen(“test.txt”,”r”);
- The following syntax is used for writing into a file.
$fp = fopen(“test.txt”,”w”);
- To open a file for appending that is To add data at the end of file.
$fp = fopen(“test.txt”,”a”);
- The fopen() function returns false, If the file can not be opened for any
reason.
If($fp=fopen(“test.txt”,”w”)
{
// code to be executed
}
or
if($fp=fopen(“test.txt”,”w”) or die(“can not open file, Sorry”);
- After doing your work on file, you should remember to close the file by
using the function fclose().
- the fclose() function requires the file pointer as argument.
- The following is the syntax.
$fp=fopen(“test.txt”,”w”);
.
.
fclose($fp);
Example Program:
Write a program to open a file for reading its content. If content is not
available , Then add some content into file.
<?php
$file='s1.txt';
if(file_exists($file))
{
$handle=fopen($file,"r");
$contents=fread($handle,filesize($file));
fclose($handle);
echo $contents;
}
else
{
$handle=fopen($file,"w");
$contents='Brad' . PHP_EOL . 'Sara' . PHP_EOL . 'Mike';
fwrite($handle,$contents);
fclose($handle);
}
?>
Reading from Files, Writing or Appennding to a File:
- PHP provides a number of functions for reading data from files.
- These functions enable you to read by the byte or by the whole line or
even by the single character.
Reading lines from a file with fgets() and feof() functions:
- When you open a file for reading, you might want to access it line by
line.
- To read a line from a file, you use the fgets() function which requires the
file pointer as argument and an integer, which specifies the number of
bytes that function should read.
- The fgets() function reads the file until it reaches a new line character.
Syntax:
$line=fgets($fp,1024);
- To know the end of file, we use feof() function. If we reach end of file,
this function returns true otherwise it returns false.
Syntax:
feof($fp)
where fp is the file pointer returned by the fopen() function.
line by line reading from a file:
<?php
$filename="s1.txt";
$fp=fopen($filename,"r") or die("Could not open file");
while(!feof($fp))
$line=fgets($fp,1024);
echo $line . "</br>";
?>
Reading arbitrary amounts of data from a file with fread() function:
Rather than reading text by the line, you can choose to read from a file in
arbitrary defined chunks.
$chunks=fread($fp,16);
<?php
$fn="s1.txt";
$fp=fopen($fn,"r") or die("Could not open a file");
while(!feof($fp))
$chunk=fread($fp,2);
echo $chunk . "<br>";
?>
Reading characters from a file with fgetc() function:
$char=fgetc($fp);
<?php
$fn="s1.txt";
$fp=fopen($fn,"r") or die("Could not open file");
while(!feof($fp))
$char=fgetc($fp);
echo $char . "<br>";
?>
Writing or Appending to a file:
When you write to a file, we use the following syntax:
$fp=fopen(“test.txt”,”w”);
When you append to a file, We use the following syntax.
$fp=fopen(“test.txt”,”a”);
Writing to a file with fwrite() or fputs() function:
- The fwrite() function accepts a file pointer and a string as arguments
and it writes into a file.
- The fputs() function also accepts a file pointer and string as arguments
and writes into a file.
this function appends into a file.
Syntax:
fwrite($fp,”Hello World”);
fputs($fp,”Another World”);
<?php
$fn="s2.txt";
$fp=fopen($fn,"w") or die("Could not open");
fwrite($fp,"hello world\n");
fclose($fp);
echo "<p> Append to the file " . $fn . "</p>";
$fp=fopen($fn,"a") or die("Could not open file");
fputs($fp,"This is another line\n");
fclose($fp);
?>
Working With Directories:
- Directory is nothing but a folder.
- In Directory, we can store all the files.
- In a Directory, There may be another directory.
The following are the operations on Directory.
1. Listing all files in a Directory.
2. Check for specific file in a directory.
3. check if the name is a directory name or file name
4. Create a directory
5. copy files between directories
Listing all files in a Directory:
- using scandir() function, we can list all the files in a directory.
- syntax: scandir($path_of_the_folder);
Ex:-