File Handling in PHP

PHP programs can read and manipulate files that reside on webservers. With PHP, you can read a text file, create a new text file, and edit the text in a file on the server. Once a file is created on a webserver, it can be accessed by any web browser. When a webserver file is edited, those edits will be seen by anyone looking at the file.

Creating and Adding Contents to a Text File with PHP

To use PHP to create a new text file, or to change the contents of an existing text file, you must:

  1. Open the file and signify that you want to write contents to the file

  2. Write text to the file

  3. Close the file

To open a file for writing, use PHP's fopen() function. This function takes two parameters: the name of the file you want to open and either ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.