This Season
 

How to Read File Contents into a String in PHP

PHP provides several methods to use the contents of a file in a script. They all have certain steps in common. These steps are identified as they appear below. If the input file is not already a text or comma delimited file, you will have to convert it before using it for input.

Related Searches:
    Difficulty:
    Moderate

    Instructions

    1. Preliminaries common to all methods

      • 1

        Note the location in the file system of the input file. The code in these instructions assumes that the file is in the same directory as the PHP script. Open a new document in a text editor and enter the characters to indicate to the server that this portion of the document is a PHP script (usually "<?" at the beginning and "?>" at the end.

      • 2

        Enter the following line to open the file:
        $F = fopen("./data.txt," r") ;

      • 3

        Continue with one choice from Section 2.

      Reading file data in various ways

      • 1

        To read data one line at a time and place it in a variable, type the text below in your document. This example will read 99 bytes of data (100-1) or an entire line of the file (whichever comes first). It prints the contents of the string variable to the screen and repeats the process until it reaches the end of the file:
        :
        while( !feof($F))
        {
        $stuff = fgets($F,100)
        echo $stuff."<BR>";
        }

      • 2

        To read data character at a time and place it in a variable, type the following (This prints the contents of the string variable to the screen and repeats the process until the end of the file is reached):

        :
        while( !feof($F))
        {
        $onech= fgetc($F);
        echo $onech."<BR>";
        }

      • 3

        Regardless of your choice above, continue with steps in section 3.

      Common steps to finish all files

      • 1

        If you have not already done so, put the end of PHP marker in your text file (usually "?>")

      • 2

        Save the file created in your text editor with the .php extension

      • 3

        If using a different computer, copy the file to the server with PHP installed and to a location accessible to a web browser.

      • 4

        Open your file to check your work.

    Tips & Warnings

    • If the data file comes from user input or an unknown source, you can use fgetss(). If the file is in delimited format, you can use fgetcsv(). If you want to read a certain amount of the file as the variable value and then repeat with the next characters, regardless of reaching the end of a line, you can use fread().

    • In the example the file was in the same directory as the script. If this is not the case, write in the actual path to the file (for example, /www/site/data.txt) or use a PHP constant to get the path (for example, DOCUMENT_ROOT)

    • All these functions have similar syntax that can be found in the function list at www.php.net. Note that php.org and php.com go to different websites that are not related to PHP scripting.

    Related Searches

    Read Next:

    Comments

    You May Also Like

    Follow eHow

    Related Ads