HTML 5 TutorialsLearn how to manipulate the file using PHP

Learn how to manipulate the file using PHP

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. <?php
    
    $filename="demo.txt";
    //Create a file demo.txt 
    $fh=fopen($filename,'w') or die("Can not open file");
    
    $content="In such a manner you can create a file";
    fwrite($fh,$content);
    fclose($fh);
    ?>
    

  8. Here is the screen shot for the code :
  9. file manipulation using php

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

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

  17. If the file exits already then it will override the same .
  18. Now to read the data from the file add the following code in index.php file .
  19. //Read the data in the file
    $fh=fopen($filename,'r');
    $content=fread($fh,filesize($filename));
    fclose($fh);
    echo '<p>Before append'.$content.'</p>';
    

  20. Here is the screen shot for the code .
  21. read from file

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

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

  26. Now lets learn to append the data in demo.txt file .
  27. Add the following code in index.php file to implement append functionality .
  28. //Append the data to the file
    $fh=fopen($filename,'a');
    $content="This is how to append data to file";
    fwrite($fh,$content);
    fclose($fh);
    

  29. 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.
  30. //Read file with appended data
    $fh=fopen($filename,'r');
    $acontent=fread($fh,filesize($filename)); //The acontent is the content tobe appended
    fclose($fh);
    echo '<p>After appended:'.$acontent.'</p>';
    

  31. Here is the screen shot for the added code :
  32. append functionality in php

  33. Now save and run the index.php file you will have the following output .
  34. v9

  35. Thus you can see the appended content in the browser window.
  36. Hence we have learn how to manipulate the file using php .
Previous articleJQuery Project
Next articleJquery Objects and Events

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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -