ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

11 – PHP File Handling: Writing to Files

In our last tutorial, we discussed on how access file using PHP. At this we’re going to focus on how write into a file. Since we know how to access files, writing into a file is really simple. To start on lesson lets create a new PHP file called “writefile.php”.  Then add the following code:

  1. <?php
  2. //specify the filename and we call it as hello.txt
  3. $testfile = 'hello.txt';
  4. //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write
  5. //and fopen return a handle as a value, and using handle is useful because we could close it
  6. if($fh = fopen($testfile, 'w')){// we are overwriting
  7.  
  8. //returns number of bytes or false
  9.     fwrite($fh, "hello");
  10.     fwrite($fh, "World");
  11.     //close the handle
  12.     fclose($fh);
  13. }else{
  14.     //if the file can't be open..and it can prevent some error might happen
  15.     echo 'Could not open the file';
  16. }
  17.  
  18. ?>

When you execute the code above, and then when you open the hello.txt file it will look like as shown below:

wr1

And if you notice that when we issue another write statement,its just pick where are left off. So introduce to us the idea of a pointer that works like a cursor.  So while the file is open, it puts it into the beginning of the file and waiting for us to type, and we do it using fwrite($fh, “hello”); and then the cursor can be found at the side of the letter “o” and then when add another  fwrite($fh, “World”); it append the second word to our first word.

 Here’s the another example.

Here in our new code you have noticed that in order for us to move the “world” into the next line we use these $greetings = “hello\r\nWorld”; because we are using windows platform. And for UNIX you can simply use $greetings = “hello\nWorld”;.

 

  1. <?php
  2.  
  3. //specify the filename and we call it as hello.txt
  4.  
  5. $testfile = 'hello.txt';
  6.  
  7. //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write
  8.  
  9. //and fopen return a handle as a value, and using handle is useful because we could close it
  10.  
  11. if($fh = fopen($testfile, 'w')){// we are overwriting
  12.  
  13. //returns number of bytes or false
  14.  
  15. $greetings = "hello\r\nWorld";//double quote matters
  16.  
  17. fwrite($fh, $greetings);
  18.  
  19. //close the handle
  20.  
  21. fclose($fh);
  22.  
  23. }else{
  24.  
  25. //if the file can't be open..and it can prevent some error might happen
  26.  
  27. echo 'Could not open the file';
  28.  
  29. }
  30.  
  31. ?>

 

The output of the code above will now look like as shown below.

wr2

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!