PHP File Inclusion & File
system
WEB TECHNOLOGIES
File Inclusion
You can include the content of a PHP file into another PHP file before the
server executes it. There are two PHP functions which can be used to
included one PHP file into another PHP file.
•The include() Function
•The require() Function
This is a strong point of PHP which helps in creating functions, headers, footers,
or elements that can be reused on multiple pages.
This will help developers to make it easy to change the layout of complete
website with minimal effort. If there is any change required then instead of
changing thousand of files just change included file.
The include() Function
The include() function takes all the text in a specified file and copies
it into the file that uses the include function.
Both include and require are identical to each other, except failure.
include only generates a warning, i.e., E_WARNING, and continue
the execution of the script.
require generates a fatal error, i.e., E_COMPILE_ERROR, and stop the
execution of the script.
PHP Filesystem
❖The filesystem functions allow you to access and manipulate
the filesystem.
❖The filesystem functions are part of the PHP core. There is
no installation needed to use these functions.
❖For accessing the files on the system, the file path will be
used.
File System functions
•PHP File Formats Support
•PHP file() Function
•PHP file_exists() Function
•PHP fopen() Function
•PHP fwrite() Function
•PHP fclose() Function
•PHP fgets() Function
•PHP copy() Function
•Deleting a file
•PHP file_get_contents() Function
PHP File Formats Support
PHP file functions support a wide range of file formats that include:
File.txt
File.log
File.custom_extension i.e. file.xyz
File.csv
File.gif, file.jpg etc
Files provide a permanent cost effective data storage solution for simple data compared to databases
that require other software and skills to manage DBMS systems.
You want to store simple data such as server logs for later retrieval and analysis
You want to store program settings i.e. program.ini
PHP file() Function
PHP provides a convenient way of working with files via its rich collection of built in functions.
PHP file_exists() Function:-his function is used to determine whether a file exists or not.
It comes in handy when we want to know if a file exists or not before processing it.
You can also use this function when creating a new file and you want to ensure that the file does not
already exist on the server.
<?php
file_exists($filename);
?>
PHP fopen() Function
The fopen function is used to open files. It has the following syntax
<?php
fopen($file_name, $mode, $use_include_path, $context);
?>
HERE,
“fopen” is the PHP open file function
“$file_name” is the name of the file to be opened
“$mode” is the mode in which the file should be opened, the table below shows the modes
Mode description
r •Read file from beginning. Returns false if the file doesn’t exist. Read only
r+ •Read file from beginning Returns false if the file doesn’t exist. Read and
write
w •Write to file at beginning truncate file to zero length, If the file doesn’t exist
attempt to create it.
•Write only
w+ •Write to fil e at beginning, truncate file to zero length, If the file doesn’t exist
attempt to create it. Read and Write
a •Append to file at end ,If the file doesn’t exist attempt to create it. Write only
a+ •Php append to file at end .If the file doesn’t exist attempt to create it,Read
and write
PHP fwrite() Function
The fwrite function is used to write files.
It has the following syntax
<?php
fwrite($handle, $string, $length);
?>
HERE,
“fwrite” is the PHP function for writing to files
“$handle” is the file pointer resource
“$string” is the data to be written in the file.
“$length” is optional, can be used to specify the maximum file length.
Create File
<?php
$fh = fopen("my_settings.txt", 'w') or die("Failed to create file");
$text = <<<_END
localhost;root;pwd1234;my_database _END;
fwrite($fh, $text) or die("Could not write to file");
fclose($fh);
echo "File 'my_settings.txt' written successfully"; ?>
PHP fclose() Function
The fclose() function is used to close a file in php which is already open
It has the following syntax.
<?php
fclose($handle);
?>
HERE,
“fclose” is the PHP function for closing an open file
“$handle” is the file pointer resource.
PHP copy() Function
The PHP copy function is used to copy files. It has the following basic
syntax. copy($file,$copied_file);
HERE,
<?php
copy('my_settings.txt', 'my_settings_backup.txt') or die("Could not
copy file");
echo "File successfully copied to 'my_settings_backup.txt'";
?>
Deleting a file
<?php
if (!unlink('my_settings_backup.txt'))
{ echo "Could not delete file";
}else
{ echo "File 'my_settings_backup.txt' successfully deleted";
}
?>
PHP file_get_contents() Function
The file_get_contents function is used to read the entire file
contents.
<?php
echo "<pre>"; // Enables display of line feeds
echo file_get_contents("my_settings.txt");
echo "</pre>"; // Terminates pre tag
?>
PHP file size and type
The filesize function returns the size of the given file. The size is specified in
bytes.
<?php
$filename = "fruits.txt";
$fsize = filesize($filename);
echo "The file size is: $fsize bytes\n";
?>
Function Description
File_exists Used to determine if a file exists or not
fopen Used to open a file. Returns a pointer to the opened file
fwrite Used to write to files
fclose Used to open closed files
fgets Used to read a file line by line
copy Used to copy an existing file
unlink Used to delete an existing file
file_get_contents Used to return the contents of a file as a string