Monday 24 November 2014

PHP file write

Write into a file with PHP



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:
  • Open the file
  • Write your data
  • Close the file

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:
  • w - Truncate the file so all data will be lost. If the file does not exist, attempt to create it.  
  • a - Writes to the end of the existing file. If the file does not exist, attempt to create it.
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:
  • resource $handle : This is the resource variable what was the return value of the fopen function. In our example it is the $file
  • string $string : This is the data what we want to write into the file. The data is simply a string.
  • int $length : This parameter is optional and limits the maximum number of characters written into the file. If you want to write “Hello World” but the length is set to 5 then only the word “Hello” will be written in the file.
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);

0 comments:

Post a Comment