Learn how to manipulate the file using PHP

1
3429

In this session today we will learn how to manipulate the file using php that is we will learn to create, read, append and write the content in the file .

  • So to learn how to manipulate the file using php follow the steps given below :

  1. First of all download and install xampp server in your machine .
  2. Now go to Xampp -> htdocs folder and in this htdocs folder create a new folder say sample .
  3. In this sample folder create a new file called as index.php .
  4. After creating this file you will have the following view :

  5. Now open index.php file with any text editor for example Notepad++ and write the following code to create a file .
  6. Here is the screen shot for the code :

  7. Now save your file .
  8. Open up the browser and enter the following url localhost/sample/index.php and you will have the following output .

  9. Here you will not see anything in the output .
  10. When you run it in the browser it will create a file demo.txt in the sample folder under htdocs folder automatically which we had mentioned in the code .
  11. Now check the sample folder in your htdocs folder and you will have the demo.txt file created .

  12. If the file exits already then it will override the same .
  13. Now to read the data from the file add the following code in index.php file .
  14. Here is the screen shot for the code .

  15. Now save index.php file and run it in the browser you will have the following output :

  16. Now when you open the demo.txt file in notepad you will have the following view :

  17. Now lets learn to append the data in demo.txt file .
  18. Add the following code in index.php file to implement append functionality .
  19. When you run it in the browser window it will not display the appended content , for that we have to add the code to read an appended data in a file.
  20. Here is the screen shot for the added code :

  21. Now save and run the index.php file you will have the following output .

  22. Thus you can see the appended content in the browser window.
  23. Hence we have learn how to manipulate the file using php .

1 COMMENT

  1. Hi Author,
    Very Nice Post on PHP tricks, I am Newbie in PHP, find your post damn helpful. Surely bookmark this post for future use. My kind request to you that please keep on write this kind of article in future also.
    Thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here