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.

You can also search for these topics, file handling in php, how to handle the file in php, php file management, how to perform file handling in php.

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.

You can also search for these topics, php manipulating files, php file manupulating vulnerability, php text file manipulation, file manipulation php security, file manipulating using php, files are supported by php.

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:

<?php
echo readfile("demo.txt");
?>

Output :-

Welcome To PHP File Tutorial by Simmanchith.com

If all you want to do is open a file and read its contents, the readfile() method is beneficial.

You can also search for these topics, readfile() function using php, webdictionary in readfile() function using php, php readfile() function example, php readfile line by line, php readfile from url.

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 :-

<?php
$myfile = fopen("demo.txt", "r") or die("Unable to open file!");
?>

Note:- In the above example, We just opened a file and we did not do any file operations like read or write.

You can also search for these topics, php open file, php open file function, php open file for reading, php open file from url, php open file for append, php open file as string, modes to open file.

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.

<?php
$myfile = fopen("demo.txt", "r");
// some code to be executed....
fclose($myfile);
?>
You can also search for these topics, php close file, php close file function, php close file handle, php close file get_contents, php close file example, php close file handle.

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.

<?php
$myfile = fopen("demo.txt", "r") or die("Unable to open file!");
$data = fread($myfile, filesize("demo.txt"));
fclose($myfile);

echo $data;
?>

Output:-

Welcome To PHP File Tutorial by Simmanchith.com

Example 2:- The PHP code below reads only 10 characters from "demo.txt" file.

<?php
$myfile = fopen("demo.txt", "r") or die("Unable to open file!");
$data = fread($myfile, 10);
fclose($myfile);

echo $data;
?>

Output:-

Welcome To

You can also search for these topics, php read file, php read file function, php read file largefile, php read file line by line, php read file not working, php read file image, php read file to string, read a single line in php, read a single character in php, php check the end-of-line in php, read a single line in php example, read a single character in php example, php check the end-of-line in php example.

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 :

<?php
$myfile = fopen("demo.txt", "r") or die("Unable to open file!");
echo fgets($myfile) . "<br />";
echo fgets($myfile);
fclose($myfile);
?>

Output :-

Welcome To
PHP File Tutorial

Note :- The file pointer is shifted on to the next line after a call to the fgets() method.

You can also search for these topics, php read single line, php fgets() function, read single line using php, read a specific line from file, read a single line from file, example for read a single line using php, how to read single line using php, php fgets() function example.

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.

<?php
$myfile = fopen("demo.txt", "r") or die("Unable to open file!");

// Print a line number
$no = 1;

// Output one line until end-of-file

while(!feof($myfile)) {
  echo $no . ". " . fgets($myfile) . "<br />";
}

fclose($myfile);
?>

Output :-

1. Welcome To
2. PHP File Tutorial
3. By Simmanchith.com

You can also search for these topics, php check end-of-file, php feof() function, php find end-of-line, php check end-of-line example, php feof() function example, php find end of line, php how to check the end-of-file.

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.

<?php
$myfile = fopen("demo.txt", "r") or die("Unable to open file!");

echo getc($myfile) . "<br />";
echo getc($myfile) . "<br />";
echo getc($myfile);

fclose($myfile);
?>

Output :-

W
e
l

Note:- You can use feof() function with a loop to read all contents of file by character by character.

You can also search for these topics, php read single character, php fgetc() function, how to read a single character, php read single character example, php getc() function example.

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")
You can also search for these topics, php fopen() function, fopen() function using php, php create file permissions, php fopen create file, php create file example.

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.

You can also search for these topics, file permissions using php, what is the benifits in php file permissions, what can php file permission do.

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 by fopen() 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".

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

fwrite($myfile, "Hello\n");
fwrite($myfile, "World!");

fclose($myfile);
?>

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 :-

Hello
World!

You can also search for these topics, php fwrite() function, fwrite() function using php, php write file permissions, php write a file, php fwrite file example.

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:

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

fwrite($myfile, "Welcome");

fclose($myfile);
?>

Both "Hello" and "World!" have vanished from the "newfile.txt" file, leaving only the data we just typed:

Welcome

You can also search for these topics, overwriting using php, php overwriting example, how to rewrite a file using php, how to overwrite a file in php.

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:

<?php
$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");

fwrite($myfile, " To\n");
fwrite($myfile, "Simmanchtih.com");

fclose($myfile);
?>
Welcome To
Simmanchtih.com

Note:- The newly added data is appended to end of the file