This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

6.4 PHP File Open/Read

  • In this chapter we will teach you how to open, read, and close a file on the server.

    PHP Open File - fopen()

    • A better method to open files is with the fopen() function. This function gives you more options than the readfile() function. We will use the text file, "Module6Quiz.txt", during the lessons:
    6.1 PHP Dates and Time - Exercises
    Started: Feb 12 at 8:06pm 
    <br>
     Question 1 1 pts
    <br>
    Use the correct date function to output the weekday of today (monday, tuesday etc.). Fill in the blank.
    <br>
    echo <input>date("l")</input>;
    <br>
     Question 2 1 pts
    <br>
    Use the correct format parameter to output a date like this: 2021.01.05. Fill in the blank.
    <br>
    echo date(<input>"Y.m.d"</input> );
    <br>
     Question 3 1 pts
    <br>
    Use the correct format parameter to output the time like this: 14:08:58 (with hour as a 24-hour format). Fill in the blank.
    <br>
    echo date(<input>"H.i.s"</input> );

    The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:

    Example 1: PHP File Open/Read - PHP Open File - fopen()
    <?php
    $myfile = fopen("Module6Quiz.txt", "r") or die("Unable to open file!");
    echo fread($myfile,filesize("Module6Quiz.txt"));
    fclose($myfile);
    ?>
    • Tip: The fread() and the fclose() functions will be explained below.
    • The file may be opened in one of the following modes:
    Modes Description
    r Open a file for read only. File pointer starts at the beginning of the file
    w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.
    a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist.
    x Creates a new file for write only. Returns FALSE and an error if file already exists.
    r+ Open a file for read/write. File pointer starts at the beginning of the file.
    w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.
    a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist.
    x+ Creates a new file for read/write. Returns FALSE and an error if file already exists.

    PHP Read File - fread()

    • The fread() function reads from an open file.
    • The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.
    • The following PHP code reads the "Module6Quiz.txt" file to the end:
    fread($myfile,filesize("Module6Quiz.txt"));

    PHP Close File - fclose()

    • The fclose() function is used to close an open file.
    • It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!
    • The fclose() requires the name of the file (or a variable that holds the filename) we want to close:
    <?php
    $myfile = fopen("include/Module6Quiz.txt", "r");
    // some code to be executed....
    fclose($myfile);
    ?>

    PHP Read Single Line - fgets()

    • The fgets() function is used to read a single line from a file.
    • The example below outputs the first line of the "include/Module6Quiz.txt" file:
    Example 2: PHP File Open/Read - fgets() Read Single Line
    <?php
    $myfile = fopen("include/Module6Quiz.txt", "r") or die("Unable to open file!");
    echo fgets($myfile);
    fclose($myfile);
    ?>

    Note: After a call to the fgets() function, the file pointer has moved to the next line.

    PHP Check End-Of-File - feof()

    • The feof() function checks if the "end-of-file" (EOF) has been reached.
    • The feof() function is useful for looping through data of unknown length.
    • The example below reads the "Module6Quiz.txt" file line by line, until end-of-file is reached:
    Example 3: PHP File Open/Read - feof() Check End-Of-File
    <?php
    $myfile = fopen("include/Module6Quiz.txt", "r") or die("Unable to open file!");
    // Output one line until end-of-file
    while(!feof($myfile)) {
      echo fgets($myfile) . "<br>";
    }
    fclose($myfile);
    ?>

    PHP Read Single Character - fgetc()

    • The fgetc() function is used to read a single character from a file.
    • The example below reads the "Module6Quiz.txt" file character by character, until end-of-file is reached:
    Example 4: PHP File Open/Read - fgetc() Read Single Character
    <?php
    $myfile = fopen("include/Module6Quiz.txt", "r") or die("Unable to open file!");
    // Output one character until end-of-file
    while(!feof($myfile)) {
      echo fgetc($myfile);
    }
    fclose($myfile);
    ?>

    Note: After a call to the fgetc() function, the file pointer moves to the next character.

    Complete PHP Filesystem Reference

    For a complete reference of filesystem functions, go to W3Schools.com complete PHP Filesystem Reference.

    Navigate this module

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    Animated PHP icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon

    Module 6 quiz