PHP - Read in a file

(1 vote, average 4.00 out of 5)

The following function demonstrates how to read in a local file with PHP. You must first use the fopen() function to open the file handle. This tells PHP where to look to find this file. After that, it is simply a matter of iterating over the file until there are no more lines. The feof() function tests if it has reached the end of the file yet - returning true if it has and false if there is still more to be read. Once we know there is another line to be read in, fgets() reads in the line for us into a slot of the array. Once all the data is read, fclose() tells PHP to close the file handle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Reads in the a file to an array
* @param $filename the name of the file to read
* @return if the file exists, it returns an array of the file contents
*/
function read($filename){
        $config = array();
        if(file_exists($filename)){
                $stdin = fopen($filename, 'r') or die("cannot find file"); // initialize the scanner
                $i = 0;
                while(! feof($stdin)){
                        $config[$i] = fgets($stdin);
                        $i++;
                }
                fclose($stdin);
                return $config;
        } else {        // the file did not exist, return null
                return null;
        }       
}
Partner Links:
Last Updated on Saturday, 17 July 2010 23:48  
Related Articles

» Java - Reading and writing a properties file

A properties file in Java can be very useful when you start to notice that there are many locations in your code where you are either hard coding values, many constants, or using enums. Instead of including these values directly in your code, you can refactor them out to a properties file to separate responsibilities. Instead of potentially specifying values throughout many classes or programs, all values can be condensed into one or more properties files as need.An example of a properties file...

» Combine MP4/M4V Files in Linux/Ubuntu

You may want to combine multiple mp4/m4v video files into one continuous video. In order to do this, you need to install the gpac library of programs onto Ubuntu. Open up Terminal and run:sudo apt-get install gpac This will install the gpac library. One of the programs included with it is MP4Box, which you can use to concatenate the video files. If you are using 64 bit Linux or get an error like MP4Box: error while loading shared libraries: libgpac.so: cannot open shared object file: No such...

» How to Use a Line Break in Excel

In some circumstances, users would like to add a newline, return, or line break to a single cell within an excel spreadsheet. Hitting enter (or return) will jump to the next cell. However, there are hot keys that allow you to do this easily.In Windows, you can use the alt-enter (or alt-return) key combination.In Mac, you can use control-option-return. If this combination does not work, try command-option-return.