Reading and Saving Data with PHP

1 post Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Reading and Saving Data with PHP
smashapps
Hello everyone,

This tutorial on saving text in PHP to a file is for the Wacky Weekend Contest #2

Wacky Weekend Contest #2

PHP 5 has a function called file_put_contents which is used to write a string to a file. For this tutorial I am not going to explain how to setup a server and assume you already have a running server with PHP. I am using Xampp and doing this on the localhost.

We will create a new variable to hold the name of the file, note when writing to the file, if it doesn't exist then it will create a new file with the name you provided.
Code: Select all
<?php
$fileName = 'codenstuff.txt';

?>
Now we want to open the file and grab everything out of it, let's create a variable to store everything in our file.
Code: Select all
<?php
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

?>
Now we will have a variable called fileContents which should contain our files contents.
We need to append text to our variable being writing it back to the file. To do this in PHP you use .= and then put the text you want to add, with \n on the end of the text so we know it's a new line.
Code: Select all
<?php
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

$fileContents .= “SmashApps\r\nCodenstuff\r\n”;
?>
The text should display as:

SmashApps
Codenstuff

Now we just need to write our text to the file.
Code: Select all
<?php
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

$fileContents .= “SmashApps\r\nCodenstuff\r\n”;

file_put_contents($fileName, $fileContents);
?>
We have so far covered how to write/save data to a file, we have read the file too but let's now display the contents on the screen.
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Reading and writing to a text file</title>
<style>
body {
	font-family:Calibri;
}
</style>
</head>
<body>
<?php
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

$fileContents .= "SmashApps\r\nCodenstuff\r\n";

file_put_contents($fileName, $fileContents);

echo "The text below is what is contained in this file: "; echo "<b>"; echo $fileName; echo "</b><p><hr></p>";
echo $fileContents;

?>
</body>
</html>
We simply echo the $fileContents variable onto the screen, I added HTML to neaten up the page a bit. It's not required obviously but makes it nicer to read on the eye. Some things to note are that every time you reload the page you are going to have the same contents written over and over again onto the page, maybe you want to just choose what you want written to the text file.

Let's create a separate page for adding text, and a separate page for reading the text.

Create two Php files called read.php and write.php

write.php will contain a form that let's you enter what text you want added to the file, and then it will take us to write text.php where the text is then written to the text file, then click the link to go to read.php to read the contents of the text file.

Write.php
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Reading and writing to a text file</title>
<style>
body {
	font-family:Calibri;
}
</style>
</head>
<body>
Fill in the textarea below then click Submit to write it to the file.
Code: Select all
<form action="write text.php" method="post">
<textarea name="content" rows="12" cols="28">
All text in here will be written to the file  on the server "codenstuff.txt"
</textarea></br>
<input type="submit" value="Submit">
</form>
This is our form which sends the message in the textarea control to our php file write text

Write text.php
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Reading and writing to a text file</title>
<style>
body {
	font-family:Calibri;
}
</style>
</head>
<body>
<?php
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

$fileContents .= $_POST["content"];

file_put_contents($fileName, $fileContents);

echo "The text has been writting to the file "; echo "<b>"; echo $fileName; echo "</b><p><hr></p>";
echo "<a href='read.php'>Click here to read the contents of the file</a>";
?>
</body>
</html>
This writes everything to the text file from the form we filled in

Read.php
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Reading and writing to a text file</title>
<style>
body {
	font-family:Calibri;
}
</style>
</head>
<body>
<?php
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

echo $fileContents;
?>
</body>
</html>
This displays the text on the screen, the only lines of code to take note of are these:
Code: Select all
$fileName = 'codenstuff.txt';

$fileContents = file_get_contents($fileName);

echo $fileContents;
fileName is the name of the file we are reading, then fileContents uses the file_get_contents function to read the file based on which file name we put in before and then echo displays the contents of the file onto the screen.

Hopefully the tutorial was easy follow, and in case you didn't follow all of it it showed how to write and save data using variables, and also by submitting the text in an HTML form.
You do not have the required permissions to view the files attached to this post.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
1 post Page 1 of 1
Return to “Tutorials”