Creating text files with PHP
PHP has file handling functions that can create, read or delete files. To to create a text file several functions are used together. The fopen() function, opens a file for writing, the fputs() function
to actually write information to a file, and the fclose() function to close the file when it is no longer
going to be used.
Function fopen(string filename, string mode)
The function expects to be given the name of file, and the reading/writing mode of the file, and returns a file handler.
Note: Modes that can be used to create a new text file are:
Mode Operations Allowed
a - append to file, create if necessary, start writing at the end of the file
r+ - read and write to file
w+ - read and write to file, create if necessary, discard previous contents if any
a+ - read and write to file, create if necessary, start writing at the end of the file
Modes with a plus sign (+) allow both reading and writing to a file.
Modes with a b as the last part of the mode (a+b), indicate that the file is a binary file.
Note: The PHP script to create a file will only execute successfully in a directory that it has read/write access to. The permission for the directory should be set to 777 (chmod: 777).
Function fputs(int file_handle, string output)
The function writes data to a file that has been opened. It expects the file handler returned by fopen(). Returns true(1) or false(0), depending on the success or failure of the attempt to write data to a file.
Note: Fputs() also takes the file hander returned by other functions that open files, such as fsockopen() or popen().
Function fclose(integer file_handle)
The function closes an open file. It expects the file handle returned by fopen(), and uses it to close the file. Returns true(1) or false(0), depending on the success or failure of the attempt to close the file.
PHP Code:
<?php $filename ="romulus.txt"; // First assign a name (and also a path if the file will not be in the // same directory) to the file that we're going to create. $myFile= fopen($filename,'a'); // Open the file for writing if(! $myFile){ // Make sure the file was opened successfully print ("File could not be opened."); exit; } $string = "According to the Roman legend, Romulus was the founder of Rome and Remus was his twin brother. Their story begins with their grandfather Numitor, king of the ancient Italian city of Alba Longa, was deposed by his brother Amulius. Numitor's daughter, Rhea Silvia, was made a Vestal Virgin by Amulius - this means that she was made a priestess of the godess Vesta and forbidden to marry. Nevertheless, Mars, the god of war, fell in love with her and she gave birth to twin sons."; // Create data to be added to the text file. fputs($myFile, $string); // Write the data ($string) to the text file fclose($myFile); // Closing the file after writing data to it ?>
See it in action. ¦ Get complete code.
The data to be written to the text file does not need to be pre-defined in the script, it can be passed to the script via a form variable.
See it in action. ¦ Get complete code.