• We just launched and are currently in beta. Join us as we build and grow the community.

PHP File Handling: Writing to Files

lalalallalal

Firewall Penetration Expert
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
170
Likes
85
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In our last tutorial, we discussed on how access file using PHP. At this time, 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. //returns number of bytes or false
  8. fwrite

    (

    $fh

    ,

    "hello"

    )

    ;
  9. fwrite

    (

    $fh

    ,

    "World"

    )

    ;
  10. //close the handle
  11. fclose

    (

    $fh

    )

    ;
  12. }

    else

    {
  13. //if the file can't be open..and it can prevent some error might happen
  14. echo

    'Could not open the file'

    ;
  15. }

  16. ?>

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


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 a windows platform. And for UNIX you can simply use $greetings = "hello\nWorld";</</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. //returns number of bytes or false
  8. $greetings

    =

    "hello\r

    \n

    World"

    ;

    //double quote matters
  9. fwrite

    (

    $fh

    ,

    $greetings

    )

    ;
  10. //close the handle
  11. fclose

    (

    $fh

    )

    ;
  12. }

    else

    {
  13. //if the file can't be open..and it can prevent some error might happen
  14. echo

    'Could not open the file'

    ;
  15. }

  16. ?>

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


Book traversal links for PHP File Handling: Writing to Files

  • ‹ PHP File Handling: Reading File
  • Up
  • PHP Foreach Loop ›

 

440,010

316,559

316,568

Top