File Handling in PHP
Using the Built in Functions like,- fopen(),
- fclose(),
- feof(),
- realpath(),
- dirname(),
- basename(), etc.
fopen() : Open a file with PHP Language, fopen() function is required to create connection between our PHP Coding file and Other file like Notepad file or we can say .txt file.
It works same as other languages built-in functions works like C Programming Language or C++ Programming Language.
Syntax of Function fopen()
fopen(filename,mode)
For Example
$file1= fopen("Hitesh.txt", "w");
We can open a file in different modes as given below
Writing the Content on to the File.
If you have a version of PHP below version 5, then you can use the fwrite() function. But you first need to use fopen( ) to get a file handle.
In the next script, we'll try to write some text to a file. We'll use the "w" option, as this will create a file for us, if we don't have one with the filename chosen.
<?PHP
$file = fopen("Hitesh.txt", "w");
fwrite($file, "Hi, This is file Handling Program of PHP");
fclose($file);
print "file created and one line of text is written inside Hitesh.txt";
?>
In Latest Version of 5 of Above version of PHP, We can use file_put_contents() Function
It is used in the same way, but has an optional third parameter:
file_put_contents($file_handle, $file_contents, context);
The context option can be FILE_USE_INCLUDE_PATH, FILE_APPEND, LOCK_EX.
So to append to the file, just do this:
file_put_contents($file_handle, $file_contents, FILE_APPEND);
Reading From the File
<?PHP
$file_contents = fopen( "Hitesh.txt", "r" );
print $file_contents;
fclose($file_contents);
print $file_contents;
fclose($file_contents);
?>
Here Print $file_content will print all the content written in the file.
Same way we can write or print the content of the file by using fgets() function which reads content line by line.
<?PHP
$file = fopen("Hitesh.txt", "r");
while (!feof($file)) {
$line_of_text = fgets($file);
print $line_of_text . "<BR>";
print $line_of_text . "<BR>";
}
fclose($file);
?>
fclose() function is used to close connection between PHP code or Hitesh.txt File.
Fopen() function is used for creating connection between PHP coding and Hitesh.txt file, and if you have created connection then your responsibility to close it after reading or writing work is completed.
Some Useful Functions for Printing FileName, FilePath and Directory on which file is there.
Create a file with filename “Hitesh.php” and Store it on to the folder “PHP1”
Print the Path of a File By Using realpath() function
<?PHP
$filepath = realpath("Hitesh.php");
print "The path of the File Hitesh.php is: " . $filepath;
?>
If You wants to get the exact path of file, then, you can use realpath(). In between the round brackets of the function, type the name of the file which is created by you Here in above example Hitesh.php is the file name and $filepath variable prints the name of the file.
Print the Folder or Directory Name, but not the file name
<?PHP
$f = dirname("PHP/Hitesh.php");
print "The folder or directory name is: " . $f . "<BR>";
?>
You can get the names of the folders, you can use the dirname( ) function. This will strip off the name of the file and return the rest of the text between the round brackets of the function.
Print the Filename By Using basename() function
<?PHP
$v = basename("PHP/Hitesh.php");
print "The File Name is: " . $v . "<BR>";
?>
In above example the $v prints the file name “Hitesh.php”.
For learning PHP PROJECT TRAINING in VADODARA, GUJARAT visit website : WWW
Vataliyatuitionclasses.com
For PHP Project training in Vadodara, gujarat, india. visit : http://www.vataliyacomputer.in/php-coding-programming-class.php
ReplyDeleteClasses often provide a collaborative environment where you can ask questions and get feedback from instructors and peers. http://www.vataliyaclasses2.blogspot.com
ReplyDeleteThe class covers a wide range of Python topics, making it suitable for beginners and those looking to enhance their coding skills.http://www.vataliyacomputer.in/our-teachers.php
ReplyDelete