Current Location: Home> Latest Articles> PHP File Operations for Beginners: Learn How to Read and Write Files in PHP

PHP File Operations for Beginners: Learn How to Read and Write Files in PHP

gitbox 2025-06-17

1. Creating and Opening Files

In PHP, you can create and open files using the fopen() function. To create a new file and open it, you can use the following code:


$myfile = fopen("testfile.txt", "w") or die("Unable to open file!");

In this example, "testfile.txt" is the file we want to create, and "w" indicates that we want to open the file in write mode. If the file already exists, its content will be truncated, and a new file will be created.

2. Reading File Content

Once the file is successfully opened, we can use the fread() function to read the content of the file. Here's a simple example:


$myfile = fopen("testfile.txt", "r") or die("Unable to open file!");
echo fread($myfile, filesize("testfile.txt"));
fclose($myfile);

This code reads the content of the "testfile.txt" file and outputs it to the page. Finally, we use fclose() to close the file.

3. Writing to Files

Unlike reading, writing to a file requires the fwrite() function. Here's an example of writing to a file:


$myfile = fopen("testfile.txt", "w") or die("Unable to open file!");
$txt = "Hello world!";
fwrite($myfile, $txt);
fclose($myfile);

In this example, we write the string "Hello world!" to the "testfile.txt" file.

4. Closing Files

After completing file reading or writing operations, you should always use fclose() to close the file. Here's an example:


fclose($myfile);

This line of code closes the file that was previously opened, ensuring the file operation is properly completed.

5. Error Handling

Errors can occur during file operations, and we can handle them using the die() function. Here's an example of error handling:


$myfile = fopen("testfile.txt", "w") or die("Unable to open file!");

If we can't open "testfile.txt", the die() function will output an error message and terminate the script.

6. Storing File Content in a Variable

Sometimes, you may need to load the content of a file into a variable. PHP provides the file_get_contents() function to achieve this. Here's an example:


$myfile = "testfile.txt";
$txt = file_get_contents($myfile);
echo $txt;

In this example, we store the filename "testfile.txt" in a variable and use file_get_contents() to read its content into the $txt variable, which is then outputted.

7. Iterating Through Each Line of a File

If you need to read a file line by line, you can use the fgets() function. Here's a simple example:


$myfile = fopen("testfile.txt", "r") or die("Unable to open file!");
while (!feof($myfile)) {
  echo fgets($myfile);
}
fclose($myfile);

This code reads the file "testfile.txt" line by line and outputs each line until the end of the file is reached.

Conclusion

In this article, we covered the basic steps for PHP file handling, including how to create, open, read, write, and close files. We also discussed error handling and how to store file content in variables. Mastering these fundamental operations is essential for PHP developers, as they allow you to handle file operations effectively in your projects.