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.
PHP - Creating Confusion
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.
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
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.
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. 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.
In the near future Tizag.com will have a more in-depth tutorial on how to use CHMOD to set
Download Tizag.com's PHP Book
If you would rather download the PDF of this tutorial, check out our
PHP eBook from the Tizag.com store.
Print it out, write all over it, post your favorite lessons all over your wall!
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!