PHP File Handling
PHP File System allows us to create file, read file line by line, read file character by
character, write file, append file, delete file and close file.
PHP Open File - fopen()
The PHP fopen() function is used to open a file.
Syntax
1. resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, r
esource $context ]] )
Example
1. <?php
2. $handle = fopen("c:\\folder\\file.txt", "r");
3. ?>
PHP Close File - fclose()
The PHP fclose() function is used to close an open file pointer.
Syntax
1. ool fclose ( resource $handle )
Example
1. <?php
2. fclose($handle);
3. ?>
PHP Read File - fread()
The PHP fread() function is used to read the content of the file. It accepts two arguments:
resource and file size.
Syntax
1. string fread ( resource $handle , int $length )
Example
1. <?php
2. $filename = "c:\\myfile.txt";
3. $handle = fopen($filename, "r");//open file in read mode
4.
5. $contents = fread($handle, filesize($filename));//read file
6.
7. echo $contents;//printing data of file
8. fclose($handle);//close file
9. ?>
Output
hello php file
PHP Write File - fwrite()
The PHP fwrite() function is used to write content of the string into file.
Syntax
1. int fwrite ( resource $handle , string $string [, int $length ] )
Example
1. <?php
2. $fp = fopen('data.txt', 'w');//open file in write mode
3. fwrite($fp, 'hello ');
4. fwrite($fp, 'php file');
5. fclose($fp);
6.
7. echo "File written successfully";
8. ?>
Output
File written successfully
Click me for more details...
PHP Delete File - unlink()
The PHP unlink() function is used to delete file.
Syntax
1. bool unlink ( string $filename [, resource $context ] )
Example
1. <?php
2. unlink('data.txt');
3.
4. echo "File deleted successfully";
Mode Description
Opens file in read-only mode. It places the file
r
pointer at the beginning of the file.
Opens file in read-write mode. It places the file
r+
pointer at the beginning of the file.
Opens file in write-only mode. It places the file
pointer to the beginning of the file and truncates
w
the file to zero length. If file is not found, it creates
a new file.
Opens file in read-write mode. It places the file
w+ pointer to the beginning of the file and truncates
the file to zero length. If file is not found, it creates
a new file.
Opens file in write-only mode. It places the file
a pointer to the end of the file. If file is not found, it
creates a new file.
Opens file in read-write mode. It places the file
a+ pointer to the end of the file. If file is not found, it
creates a new file.
Creates and opens file in write-only mode. It
x places the file pointer at the beginning of the file.
If file is found, fopen() function returns FALSE.
It is same as x but it creates and opens file
x+
in read-write mode.
Opens file in write-only mode. If the file does not
exist, it is created. If it exists, it is neither
c truncated (as opposed to 'w'), nor the call to this
function fails (as is the case with 'x'). The file
pointer is positioned on the beginning of the file
It is same as c but it opens file in read-
c+
write mode.
5. ?>
PHP Open File
PHP fopen() function is used to open file or URL and returns resource. The fopen()
function accepts two arguments: $filename and $mode. The $filename represents the file
to be opended and $mode represents the file mode for example read-only, read-write,
write-only etc.
Syntax
1. resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, r
esource $context ]] )
PHP Open File Mode
PHP Open File Example
1. <?php
2. $handle = fopen("c:\\folder\\file.txt", "r");
3. ?>
PHP Read File
PHP provides various functions to read data from file. There are different functions that
allow you to read all file data, read data line by line and read data character by character.
The available PHP file read functions are given below.
o fread()
o fgets()
o fgetc()
PHP Read File - fread()
The PHP fread() function is used to read data of the file. It requires two arguments: file
resource and file size.
Syntax
1. string fread (resource $handle , int $length )
$handle represents file pointer that is created by fopen() function.
$length represents length of byte to be read.
Example
1. <?php
2. $filename = "c:\\file1.txt";
3. $fp = fopen($filename, "r");//open file in read mode
4.
5. $contents = fread($fp, filesize($filename));//read file
6.
7. echo "<pre>$contents</pre>";//printing data of file
8. fclose($fp);//close file
9. ?>
Output
this is first line
this is another line
this is third line
PHP Read File - fgets()
The PHP fgets() function is used to read single line from the file.
Syntax
1. string fgets ( resource $handle [, int $length ] )
Example
1. <?php
2. $fp = fopen("c:\\file1.txt", "r");//open file in read mode
3. echo fgets($fp);
4. fclose($fp);
5. ?>
Output
this is first line
PHP Read File - fgetc()
The PHP fgetc() function is used to read single character from the file. To get all data
using fgetc() function, use !feof() function inside the while loop.
Syntax
1. string fgetc ( resource $handle )
Example
1. <?php
2. $fp = fopen("c:\\file1.txt", "r");//open file in read mode
3. while(!feof($fp)) {
4. echo fgetc($fp);
5. }
6. fclose($fp);
7. ?>
Output
this is first line this is another line this is third line
PHP Write File
PHP fwrite() and fputs() functions are used to write data into file. To write data into file,
you need to use w, r+, w+, x, x+, c or c+ mode.
PHP Write File - fwrite()
The PHP fwrite() function is used to write content of the string into file.
Syntax
1. int fwrite ( resource $handle , string $string [, int $length ] )
Example
1. <?php
2. $fp = fopen('data.txt', 'w');//opens file in write-only mode
3. fwrite($fp, 'welcome ');
4. fwrite($fp, 'to php file write');
5. fclose($fp);
6.
7. echo "File written successfully";
8. ?>
Output: data.txt
welcome to php file write
PHP Overwriting File
If you run the above code again, it will erase the previous data of the file and writes the
new data. Let's see the code that writes only new data into data.txt file.
1. <?php
2. $fp = fopen('data.txt', 'w');//opens file in write-only mode
3. fwrite($fp, 'hello');
4. fclose($fp);
5.
6. echo "File written successfully";
7. ?>
Output: data.txt
hello
PHP Append to File
If you use a mode, it will not erase the data of the file. It will write the data at the end of
the file. Visit the next page to see the example of appending data into file.
PHP Append to File
You can append data into file by using a or a+ mode in fopen() function. Let's see a simple
example that appends data into data.txt file.
Let's see the data of file first.
data.txt
welcome to php file write
PHP Append to File - fwrite()
The PHP fwrite() function is used to write and append data into
1. <?php
2. $fp = fopen('data.txt', 'a');//opens file in append mode
3. fwrite($fp, ' this is additional text ');
4. fwrite($fp, 'appending data');
5. fclose($fp);
6.
7. echo "File appended successfully";
8. ?>
Output: data.txt
welcome to php file write this is additional text appending data
PHP Delete File
In PHP, we can delete any file using unlink() function. The unlink() function accepts one
argument only: file name. It is similar to UNIX C unlink() function.
PHP unlink() generates E_WARNING level error if file is not deleted. It returns TRUE if
file is deleted successfully otherwise FALSE.
Syntax
1. bool unlink ( string $filename [, resource $context ] )
$filename represents the name of the file to be deleted.
PHP Delete File Example
1. <?php
2. $status=unlink('data.txt');
3. if($status){
4. echo "File deleted successfully";
5. }else{
6. echo "Sorry!";
7. }
8. ?>
Output
File deleted successfully