Pin Me

Modifying Files in PHP

written by: •edited by: Michele McDonough•updated: 7/16/2010

PHP is a very powerful scripting language. Using PHP, you can perform various file operations. In this tutorial, I will show you how you can create, read, edit and delete files using PHP.

  • slide 1 of 4

    PHP File Operations

    Modifying Files in PHP Some of the most basic file operations in PHP involve the creation, modification, and deletion of files. In this guide, I'll show how to perform all these functions and include examples to illustrate the process. I'll start out by looking at how to create files using PHP.

  • slide 2 of 4

    How to Create a File Using PHP

    To create a file using PHP, you need to use the fopen() function.

    The fopen() function takes two parameters: the file name and the reason for opening that file.

    For example, to create a file named "pat.txt" in order to write data into it, use this command

    <?

    php$filename = "pat.txt";

    $patfile = fopen($filename, 'w');

    fclose($patfile);

    ?>

    In the above code, we create the file "pat.txt" and then close the open file using the fclose() command.

    The fopen() command returns a file handle which you can then use to manipulate the file to edit, delete and modify it.

    The fclose() command closes the file using that file handle. After you have finished working with a file, you should always close it.

    Besides creating a file, the fopen() command is also used to open already existing files. To open a file, you need to specify a mode: read, write or append, depending on what you are opening the file for.

    The syntax to open an existing file is

    $filehandle = fopen($filename, 'mode');

    Types of File Modes

    • r: Open the file for read-only. File Pointer at the start of file.
    • w: Open the file for write only. File Pointer at the start of the file.
    • a: Open the file to append text at the end. File pointer at the end of the file.

    Similarly, r+ can be used to both read and write from the file, w+ is the same as r+ but all the data is deleted when the file is opened. a+ is like r+ but the data isn't deleted, and the pointer is at the end of the file.

    For example, to open the pat.txt file for appending data to it,

    $patfile = fopen("pat.txt", 'a');

  • slide 3 of 4

    How to Edit a File Using PHP

    To edit a file using PHP, you need to first open it using a suitable mode and then edit it.

    To add text to the end of the file, open it in the 'a+' mode. To delete all the data and add new text to the file, open it in 'w+' mode. Now after you have opened a file, to write data to it, use the fwrite() function.

    The syntax of fwrite() is

    fwrite($filehandle, $textdata);

    The $textdata variable is the string of text you want to insert into the file.

    <?php

    $filename = "pat.txt";

    $textdata = "I'm adding this data to the file";

    $patfile = fopen($filename, 'a+');

    fwrite($patfile, $textdata);

    fclose($patfile);

    ?>

    As we have used the a+ mode, the string will be added to the file after the existing content. To read data from a file, use the fread() function.

    Syntax:

    $newdata = fread($filehandle, size);

    The fread() function returns a string with the data read from the file. It needs the file handle and the no of characters to be read as parameters.

    <?php

    $filename = "pat.txt";

    $patfile = fopen($filename, 'r+');

    $newdata = fread($patfile, 10);

    echo $newdata;

    fclose($patfile);

    ?>

    This will read the first 10 characters of the file pat.txt.

  • slide 4 of 4

    How to Delete a File Using PHP

    To delete a file, you just need to use the unlink command.

    Syntax:

    unlink($filehandle);

    To delete the file pat.txt, just use

    <?php

    $filename = "pat.txt";

    unlink($filename);

    ?>