Press enter to see results or esc to cancel.

PHP Lesson 17 – How to write files with example in PHP

Share this
PHP Lesson 17 - How to write files with example in PHP - PHP Lesson 17 - How to write files with example in PHP - PHP Lesson 17 - How to write files with example in PHP - How to write files with example in PHP
How to write files with example in PHP

To be able to write to text files, PHP has the function file_put_contents ($ filename, $ data), As the first argument, the function expects the file name, as second the data which one wants to write into the file. If the file does not exist yet, it will be created. If it already exists, the content will be overwritten.

Overwrite file in PhP

For example, if you want to write a variable passed by GET to a text file, you can:

$name = $ _GET ["name"];
$row = "The name $ name was passed via GET \ r \ n";
file_put_contents ("example.txt", $line);
echo "example.txt has been overwritten";

If we call this file with the parameter?Name=Max, then in the example.txt the sentence Per GET will have the name Max passed. The file_put_contents () function overwrites the contents of the file. If the file already exists, the content is lost.

Of course, we can also read out a file before we overwrite it. For example, we would like to program a simple visitor counter. For this creates in the folder with our PHP script by editor a text file counter.txt and writes a 0 in this. Our script for reading this file, increasing the number, and outputting the text looks like this:

$counter = file_get_contents ("counter.txt"); // reading the value
$Counter ++; // increase value by 1
file_put_contents ("counter.txt", $counter); // save the value
echo "This file has already been called $counter times";

Append text to file

If you do not want to overwrite a file, but append a value to the file, you have to pass the constant FILE_APPEND to the function  file_put_contents ()  as the third argument.

$name = $ _GET ["name"];
$row = "The name $name was passed via GET \ r \ n";
file_put_contents ("example.txt", $line, FILE_APPEND);
echo "example.txt updated";

If we call this script with different GET parameters, for example first with ? Name = Max , then with ? Name = Lena , we find for each call a new line in the example.txt.

Working with implode and explode

In the article on PHP arrays, you already got to know the two useful functions implode() and explode(), with which you can merge an array into a string or divide a string with separators. These two functions will be very useful when working with text files.

Below we build a very simple registration and an internal area. This example is very uncertain and should not be used in real systems. But it illustrates well how implode() and explode() can be used with files.

Step 1: Registration

First, we need a form in which we ask for the name, e-mail address and password. We then attach this information to users.txt .

The page registration.php looks like this:

<form action="register.php" method="post">

Name: <input type="text" name="name" /><br />

E-Mail: <input type="text" name="email" /><br />

Password: <input type="password" name="password" /><br />

<input type="submit" value="Register" />

Then the registration.php, which appends the data from the form to the file users.txt :

$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['passwort'];
$user_info = array($email, $passwort, $name);
if(!empty($name) AND !empty($email) and !empty($passwort)) {
   $entry= implode(";", $user_info)."\r\n";
   file_put_contents("users.txt", $entry, FILE_APPEND);
   echo "$email was successfully registered";
} else {
   echo "Please fill in all fields";

If all three fields have been filled, we connect the three elements by implode () and use the semicolon as separator. At the end of each entry, we write a line break (for Windows per \ r \ n) and add this entry via file_put_contents () to the end of users.txt . In the users.txt, there should be a user entry in the format email, password; name in each line.

For the loginformular.php we ask the e-mail address and the password:

<form action="login.php" method="post">

E-Mail: <input type="text" name="email" /><br />

Password: <input type="password" name="password" /><br />

<input type="submit" value="Login" />

Somewhat more complicated is the login.php. Here we go through all the lines, check if the e-mail address and the password are correct. If so, we welcome the user by his name:

$email = $_POST["email"];
$password = $_POST["password"];
$users = file("users.txt");
foreach($users AS $line)  {
   $user_info = explode(";", $line);
   if($user_info[0] == $email AND $user_info[1] == $password) {
       echo "Hello ".$user_info[2];

First, we load all lines of  users.txt per file (“users.txt”). We then go through these lines using the foreach loop. The variable  $line then always contains an entry in the format email; password; name. If the entered e-mail address and the entered password from the login form match the entry in the  users.txt, we welcome the user with his name.

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this