PHP File Handling
File handling is an essential component of every web application. You frequently need to open and process a file for various activities.
PHP Manipulating Files
Create, read, upload, and modify files are all supported by PHP.
Take caution to manipulate files! , You must be very careful while manipulating files.
If you do anything poorly, you may do a lot of damage. The most common mistakes are to modify a false file, overload a hard disc with waste data, and accidentally delete the contents of a file.
List Of PHP File Methods
PHP provides many useful functions to work with files contents (open, close, read, write) and directories.
Function | Description |
---|---|
readfile() |
Reading all file contents at once. It cannot support any File Modes. |
fopen() |
Opening a file for reading or writing. |
fclose() |
Closing a opened file. |
fread() |
Reading all file contents at a time. |
fwrite() |
Writing data to a new file or an existing file. |
fgets() |
Reading file data line by line. |
fgetc() |
Reading file data charcter by character. |
PHP readfile() Function
Reads a file and publishes it to the output buffer with the readfile()
method.
Note:- The readfile()
method returns the number of bytes reads on success.
Let's assume we have a text file on the server called "demo.txt" that looks like this :
Welcome To
PHP File Tutorial
by Simmanchith.com
Example :- The following is the PHP code for reading the file and writing it to the output buffer:
Output :-
If all you want to do is open a file and read its contents, the readfile()
method is beneficial.
List Of PHP File Modes
File Modes are very important thing to work with any files. It tells what type of action should be performed while reading or writing on the files.
One of the following modes can be used to open the file:
Modes | Description |
---|---|
r | Allow read-only access to a file. The beginning of the file is indicated by the file pointer. |
w | Just open a write only file. If the file doesn't exist, it will be created. If the file exist it overwrites the existing file contents. |
a | Open a writing only file. The data in the file is not overwritten. If the file doesn't exist, it will be created. If the file exist it appends the content to end of file.. |
x | This command creates a new write-only file. If the file already exists, the function returns FALSE and an error. |
r+ | Open a read/write file. The specified file must be exist. |
w+ | Open a read/write file. If the file doesn't exist, it will be created. It overwrites the existing contents. |
a+ | Open a read/write file. The data in the file already exists. It append the data to the end of file. |
x+ | This function introduces a unique read/write file. If the file existing returns FALSE and an error. |
PHP Open File - fopen()
Using the fopen()
function to open files is a better option. This method provides additional options than readfile()
.
Syntax:-
fopen("filename", "filemode");
The name of the file to be opened is the first parameter of fopen()
, and the
second argument defines the mode in which the file should be opened. If the fopen()
function
fails to open the supplied file, then the die()
function will display the message
"Unable to open file!".
Example :-
Note:- In the above example, We just opened a file and we did not do any file operations like read or write.
PHP Close File - fclose()
An open file can be closed with the fclose()
function.
After finishing it, it is a great programming habit to close all files. You don't want an open file to operate on the resource of your server!
Syntax:-
fclose("filename");
The filename (or a variable containing the filename) we wish to close by fclose()
function.
Related Links
PHP Read File - fread()
The fread()
method reads data from a file that is currently open.
The filesize()
return file size (number of bytes available) of a given file.
Syntax:
fread(file_pointer, "number_of_bytes_to_read");
- file_pointer - The file referenced by
fopen()
function - number_of_bytes_to_read - How many bytes to read from the specified file.
Example 1:- The PHP code below reads the entire text from "demo.txt" file.
Output:-
Example 2:- The PHP code below reads only 10 characters from "demo.txt" file.
Output:-
PHP fgets() Method - Read Single Line
The fgets()
method is used to read a single line from the file.
Example :- The first two line of the "demo.txt" file is printed in the example below :
";
echo fgets($myfile);
fclose($myfile);
?>
Output :-
PHP File Tutorial
Note :- The file pointer is shifted on to the next line after a call to the fgets()
method.
PHP Check End-Of-File - feof() Method
The feof()
function is used check whether a file pointer reached end of the file or not.
The feof()
function determines whether or not the "end-of-file" (EOF) condition has been met.
It is very useful when you want to read all contents of a file line by line.
For looping through data of uncertain length, the feof()
method is handy.
Example :- The following example reads line by line the file "demo.txt" until end of file.
";
}
fclose($myfile);
?>
Output :-
2. PHP File Tutorial
3. By Simmanchith.com
Related Links
PHP fgetc() Method - Read Single Character
When reading a single character from a file, the fgetc()
method is utilised.
Example :- The following example reads first three characters of the "demo.txt" file.
";
echo getc($myfile) . "
";
echo getc($myfile);
fclose($myfile);
?>
Output :-
e
l
Note:- You can use feof()
function with a loop to read all contents of file by character
by character.
PHP Create File - fopen()
A file is also created using the fopen()
method. Although it may appear to be a bit perplexing, in PHP, a file is created using the same function that opens files.
If you have used fopen()
on a file not available, it will be created once the file is
opened to write (w) or append (a) mode.
Example :- In the following example you create a new "testfile.txt" file. The PHP code file is generated in the same directory :
$myfile = fopen("testfile.txt", "w")
File Permissions
If you're experiencing trouble getting this code to execute, make sure you've given your PHP file permission to write data to the hard disc.
PHP Write to File - fwrite()
Writing to a file is done with the fwrite()
function.
Syntax:-
fwrite(file_pointer, data_to_write);
file_pointer
- The file referenced byfopen()
method tell where to write the data.data_to_write
- What data to writtern to the specified file.
Example :- The following example creates a new file named "newfile.txt".
Note:- You can use multiple times fwrite()
function to write data to file.
The newfile.txt does not exist and it will created by fopen()
function.
This is what the "newfile.txt" file looks like when we open it :-
World!
PHP Overwriting Existing File Data
The w
file mode is used to overwrite the content of a file.
Now that newfile.txt includes some information, and if you write again some data to the file it will delete the existing data and write the new one.
Example :- We'll open our old file "newfile.txt" and add some new data to it in the example below:
Both "Hello" and "World!" have vanished from the "newfile.txt" file, leaving only the data we just typed:
PHP Append Data To An Existing File
The a
file mode is used to append the content to the end of file..
Example :- We'll open our old file "newfile.txt" with a
(append mode) and add some new data to it in the example below:
Simmanchtih.com
Note:- The newly added data is appended to end of the file