Learn how to manipulate the file using PHP

1
3235

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. manipulation of file

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

  9. Now save your file .
  10. Open up the browser and enter the following url localhost/sample/index.php and you will have the following output .
  11. various manipulation on file

  12. Here you will not see anything in the output .
  13. 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 .
  14. Now check the sample folder in your htdocs folder and you will have the demo.txt file created .
  15. create file in php

  16. If the file exits already then it will override the same .
  17. Now to read the data from the file add the following code in index.php file .
  18. Here is the screen shot for the code .
  19. read from file

  20. Now save index.php file and run it in the browser you will have the following output :
  21. output of reading file in php

  22. Now when you open the demo.txt file in notepad you will have the following view :
  23. file manipulation in php

  24. Now lets learn to append the data in demo.txt file .
  25. Add the following code in index.php file to implement append functionality .
  26. 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.
  27. Here is the screen shot for the added code :
  28. append functionality in php

  29. Now save and run the index.php file you will have the following output .
  30. v9

  31. Thus you can see the appended content in the browser window.
  32. 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