Creating Text Files Using PHP

Posted by in Articles, PHP, Programming

This tutorial might come pretty handy to those who don’t want MySQL database to store their data. There is always an alternative.  In case you don’t want MySQL or any other database system to manage your data then you can have txt file to store it. Later you can fetch out the data stored in it.

To create interact with files we’ll be using following function:

fopen()

Using the PHP file system function fopen(), we can easily create a new text file. Let me show you this function more closely.

The function fopen() takes two arguments, the filename and the mode to either open or create a file.

Look at the example below:

fopen ($filename, $mode);

There are two arguments in variable $filename and $mode.

  • $filename – the name of the file. This may also include the absolute path where you want to create the file. Example, “/www/myapp/file.txt”.
  • $mode – mode is used to specify how you want to create the file. For example, you can set the mode to create for read only, or create a file for read and write.

For the variable $mode you can apply several inputs. Refer to the table below that might help you a lot.

w‘  -> Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, it attempt to create it.

w+‘ -> Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

a‘ -> Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

a+‘ -> Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

After the files have been created, we’ll need to write it to record the specific data.

We’ll be using fwrite() function to write the specific file.

Further clarification for the above function:

fwrite($Handle, $Data);

$Handle variable is used to open the file.(i.e. fopen() function will be used to open the file) and $Data variable is your record or input that you may want to keep in a file.

Now as we are clear with the basic principles, let’s head towards the topic.

To create a text file you must code with the functions we defined above.

For example,

<?php
$file = fopen("mytext.txt", "w+");
if($file==false)
        die("unable to create file");
?>

The code above creates a new file mytext.txt for writing and reading in the current directory. When you create a file, the function fopen() returns a file pointer. If the attempt to create a file fails for any reason, the function returns false with a message, ‘Unable to create file’.

The mode “w+” creates a new file for reading and writing, places the pointer in the beginning of the file. If the file already existed, it deletes everything from the file.

Similarly, you can use mode ‘a’ and ‘a+’ for creating new files. However with these modes, if the file already exists, it will not truncate the file (i.e. it will not delete any content in the file) and places the pointer at the end of the file for writing.

After the files have been created, lets write it.

See the example below:

<?php
$File = “myfile.txt”;
$Handle = fopen($File, ‘w’);
$Data = “This is the data to be recorded in myfile.txt\n”;
fwrite($Handle, $Data);
$Data = “This data stores another line to the text\n”;
fwrite($Handle, $Data);
print “Data Written”;
fclose($Handle);
?>

At the end of the file we use fclose() to close the file we have been working with. You may also notice we are using \n at the end of our data strings. The \n serves as a line break, like hitting the enter or return key on your keyboard when you are typing in notepad.

If the above example becomes successful to do its job then it willl say “Data written”.

The above example will write on myfile.txt with the following values.

This is the data to be recorded in myfile.txt

This data stores another line to the text

Hope you liked this tutorial. Next time I will tell you about reading the files that you’ve created.