PHP - File Handling

This PHP tutorial is completely dedicated to how PHP can interact with files. After completing this section you should have a solid understanding of all types of file manipulation in PHP. Manipulating files is a basic necessity for serious programmers and PHP gives you a great deal of tools for creating, uploading, and editing files.

When you are manipulating files you must be very careful because you can do a lot of damage if you do something wrong. Common errors include editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file’s contents.

It my hope that you will be able to avoid these and other slipups after reading this tutorial. However, we know that there are so many places where code can take a wrong turn, so we urge you to take extra care when dealing with files in PHP.

 
PHP - File Handling Overview

The presentation of the file lessons will begin with how to create, open, and close a file. After establishing those basics, we will then cover other important file tasks, such as: read, write, append, truncate, and uploading files with PHP.

 
PHP - File Create

Before you can do anything with a file it has to exist! In this lesson you will learn how to create a file using PHP. In PHP, a file is created using a command that is also used to open files. It may seem a little confusing, but we’ll try to clarify this conundrum.

In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending (more on this later).

 
PHP - How to Create a File

The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file (i.e. read from the file, write information, etc).

Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.

<?php
 
  $ourFileName = "testFile.txt";
  $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
  fclose($ourFileHandle);
?>

The file “testFile.txt” should be created in the same directory where this PHP code resides. PHP will see that “testFile.txt” does not exist and will create it after running this code. There’s a lot of information in those three lines of code, let’s make sure you understand it.

   $ourFileName = "testFile.txt";

Here we create the name of our file, “testFile.txt” and store it into a PHP String variable $ourFileName.

   $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

This bit of code actually has two parts. First we use the function fopen and give it two arguments: our file name and we inform PHP that we want to write by passing the character “w”.

Second, the fopen function returns what is called a file handle, which will allow us to manipulate the file. We save the file handle into the $ourFileHandle variable. We will talk more about file handles later on.

    fclose($ourFileHandle);

We close the file that was opened. fclose takes the file handle that is to be closed. We will talk more about this more in the file closing lesson.

 
PHP - Permissions

If you are trying to get this program to run and you are having errors, you might want to check that you have granted your PHP file access to write information to the hard drive. If you running code on local system and OS is Windows then most probably you may not get any problem but if it has Linux OS then you also have to set permissions.

But If you testing code on a real server server then setting permissions is most often done with the use of an FTP program to execute a command called CHMOD. Use CHMOD to allow the PHP file to write to disk, thus allowing it to create a file.

The see next part of this tutorial click here