Reading in Files

Reading in files is almost the same as writing to files—just the opposite. When we were writing to files earlier, we passed a string as a parameter that contained the data in the file. When we read in files, the reading function returns a string that we can manipulate for our purposes.

fread()

So what is the reading function, then? If you’ve been paying attention, you can probably guess the name: fread(). Take a look at the function declaration:

fread($handle, $lengthInBytes)

Both of these parameters are required. The first parameter is the handle to the file we retrieved by using fopen(). The second parameter is the number of bytes you wish to retrieve. fread() only works for pages that have been opened for reading access.

This sounds all well and good if you know the amount of data you need to retrieve, but what if you don’t? What if you want to retrieve the entire file? Fortunately, PHP gives you a way to do this. PHP’s filesize()function returns a file’s size in bytes. Look at the declaration:

filesize($filename)

Pretty cool, huh? If you want to read in an entire file, you can do it as follows:

$contents = fread($handle, filesize($filename));

There are a couple other cool functions you should know about. One is fgets(), which reads the file until the next carriage return, returning the current line. You can call this inside a loop and display or act upon the entire contents of a file.

file()

Another cool function is file(), which takes the entire file into an array without requiring an fopen() command! All you have to do is pass file() the filename, as follows:

$entireFile = file("yourfile.txt");

This creates an array of each line of the file. As an alternative, you can use a foreach loop and act upon each line of the file.

After running this line of code, the $contents variable contains the entire contents of the $handle file. The contents are in string format. You can do all of your operations upon the strings, as I explain in Chapter 8.

So let’s use all of this in a script. You know by now how to make the HTML document, but feel free to take a look at phpft14-03.html to see how the HTML looks. The page we made looks like the one in Figure 14.11.

Figure 14.11. The phpft14-03.html reading form.


Now look at the PHP code that makes the reading happen:

<?

$filename = "c:\test.txt";//$_POST['file'];

$handle = fopen($filename, 'r'),
//if there is a problem with opening file, echo out error
if (!$handle)

   {
   exit("Could not open file!");
   }

$contents = fread($handle, filesize($filename));

//If there is an error with writing, echo out an error
if (!$contents)
   {
   exit("Could not read data!");
   }

echo "The contents of your file $filename follow.<p>";
echo $contents;

fclose($handle);

?>

The main thing is the fread() line:

$contents = fread($handle, filesize($filename));

Here, we pass the $handle and the filesize of the file to the fread() function. We put the result of this operation into the $contents variable. After this we check to see if $contents is valid by running the following if test:

if (!$contents)
  {
  exit("Could not read data!");
  }

$contents is invalid if it is blank, so we can check to see if it is valid by doing the above test. If the file is blank, the program exits. Cool, huh? You can do whatever you like with the file, maybe remove all of the new lines, or search for specific substrings, or whatever you feel like!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.186.201