PHP File Handling
PHP File Handling
PHP File handling is an important part of any web application. You often need to open and process a file for different tasks.
- Opening a File
- Reading a File
- Writing a File
- Closing a File
- Delete a File
Opening a File using fopen()
Function
A better method to open files is with the fopen()
function. This function gives you more options than the readfile()
function.fopen()
returns a file handle resource, which is basically the contents of the file.
<?php
$myfile = fopen("file.txt", "w");
?>
The file may be opened in one of the following modes
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
Read a File using fread()
Function
The fread()
function reads from an open file.
The first parameter of fread()
contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read. fread($file,filesize("test.txt"))
;
<?php
$file=fopen("test.txt","r");
$contents = fread($file, filesize("file.txt"));
fclose($file);
print $contents;
?>
Reading a File Line by Line using fgets()
Function
The fgets()
function is used to read a single line from a file. After the line has been read the file pointer is pointing to the next line in the file. This is very useful, because we could now read the file line by line.
<?php
$file=fopen("test.txt","r") or exit("Unable to open the file!");
echo fgetc($file);
?>
Reading a File Character by Character Using fgetc()
Function
The function fgetc(
) can be used to read a character from a file.
<?php
$file=fopen("test.txt","r") or exit("Unable to open the file!");
echo fgetc($file);
?>
Writing a File using fwrite()
Function
The fwrite()
function is used to write to a file. The first parameter of fwrite()
contains the name of the file to write to and the second parameter is the string to be written.
<?php
$myfile = fopen("file.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
echo "file is created";
fwrite($myfile, $txt);
?>
Closing a File using fclose()
Function
The fclose()
function is used to close an open file.
<?php
$file = fopen("test.txt", "r");
fclose($file);
?>
Delete a File using unlink()
Function
When you view the contents of a directory you can see all the files that exist in that directory because the operating system or application that you are using displays a list of filenames. You can think of these filenames as links that join the files to the directory you are currently viewing.
If you unlink a file, you are effectively causing the system to forget about it or delete it!
Before you can delete (unlink) a file, you must first be sure that it is not open in your program. Use the fclose function to close down an open file.
<?php
$file = "test.txt";
unlink($file);
?>
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |