PHP file write


Home - Tutorials - Basics

Creating files with PHP is quite simple task but you need to be careful during file manipulations. In this lesson I will show you how to do it without headache.

Tutorial info:


Name:PHP file write
Total steps:1
Category:Basics
Level:Beginner

Bookmark PHP file write



Bookmark and Share

Step 1 - Write into a file with PHP


PHP file write

In cases when you want to store some data, log user activity or anything else you may want to store this information in a file. To do this there are 3 basic steps you need to do:


Step 1.

If you want to write into a file you need to open it for writing. You have 2 possibilities depending on what you need:

If opening of the file was not success then fopen returns with FALSE

So the PHP code looks like this:

Code:
  1. $fileName = 'test.txt';
  2. $file = fopen($fileName,'w');
  3. if ($file === FALSE) {
  4.     echo "Can’t open file!";
  5. }
  6.  

If the filename is not only a simple file but contains a path as well then you should use the built in DIRECTORY_SEPARATOR constant to be cross platform compatible as follows:

Code:
  1. $fileName = 'log' . DIRECTORY_SEPARATOR . 'error' . DIRECTORY_SEPARATOR . 'error.log';

Don’t forget that if the file doesn’t exist then it will be created only if the path exists. So if you use the above example and the log/error directory doesn’t exists then you will get an error.

Step 2.

At this point we have a valid file resource and so we can use it to write some data into it.
To write into a file you need to use the fwrite() function.

The synatx:

Code:
  1. int fwrite ( resource $handle , string $string [, int $length ] )

Explanation:

Now try to write out a simple message:

Code:
  1. $text = "This is a simple message";
  2. fwrite($file, $text);
  3.  

Now if you want to write an other text you simply call fwrite again:

Code:
  1. fwrite($file, “Other text”);

If you checks the content of the file you will see the following:

Code:
  1. This is a simple messageOther text

Everything is in one line. If you want to write in separate lines then you need to write newline characters also. You can do this in a cross platform compatible way by using the built in constant PHP_EOL. So the code looks like this:

Code:
  1. $text = "This is a simple message" . PHP_EOL;
  2. fwrite($file, $text);
  3. fwrite($file, "Other text" . PHP_EOL);
  4.  

Step 3.

This is a very simple step, you simply need to close the file with fclose as here:

Code:
  1. fclose($file);

That's all about writing into a file with PHP.

The complete code looks like this:

Code:
  1. $fileName = 'test.txt';
  2. $file = fopen($fileName,'w');
  3. if ($file === FALSE) {
  4.     echo "Can’t open file!";
  5. }
  6.  
  7. $text = "This is a simple message" . PHP_EOL;
  8. fwrite($file, $text);
  9. fwrite($file, "Other text" . PHP_EOL);
  10.  
  11. fclose($file);







Tags: php file write, write into files, php, file, write, save, append to file, create new file

PHP file write - Table of contents
Step 1 - Write into a file with PHP


Follow phpf1 on Twitter




F1 Site Family
AJAX F1
CSS F1
Database F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.0262