Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to PHP and MySQL (University at Buffalo Version)


Chapter 7: Working With Files

File Input/Output (I/O)

PHP provides many functions for working with files.

Managing a File Handle Using the fopen() and fclose() Functions

To work with a file using PHP you need to create a file handle using the fopen() function. To destroy a file handle use the fclose() function.

Reading a File Using the PHP fscanf() Function

You can use the fscanf() function to read a file into an array one line at a time.

<?php
$fh = fopen("socsecurity.txt", "r");
// Parse each SSN in accordance with integer-integer-integer format.
while ($user = fscanf($fh, "%d-%d-%d")) {
  list ($part1, $part2, $part3) = $user;
  echo("SSN = $part1-$part2-$part3<br/>\n");
}
fclose($fh);
?>

Figure 7-1: The fopen(), fscanf() and fclose() Functions
SSN = 123-45-6789
SSN = 234-56-7890
SSN = 345-67-8901

Figure 7-2: The fopen(), fscanf() and fclose() Output

Reading a CSV File Using the PHP fgetcsv() Function

You can use the fgetcsv() function to read a file one line at a time.

<?php
$fh = fopen("subscribers.csv", "r");
// Parse each of comma-separated values.
$thisLine = "<p>";
while (list ($name, $email, $phone) = fgetcsv($fh, 1024, ",")) {
  $thisLine .= "<b>Name:</b> $name <b>Email:</b> ($email) <b>Telephone Number:</b> $phone<br/>\n";
}
$thisLine .= "</p>";
echo($thisLine);
fclose($fh);
?>

Figure 7-3: The fgetcsv() Function

Name: Jim Gerland Email: (gerlanjr@buffalostate.edu) Telephone Number: (716)123-4567
Name: Jim Gerland Email: (gerlnd@buffalo.edu) Telephone Number: (716)456-7890
Name: Jim Gerland Email: (jim.gerland@thegerlands.org) Telephone Number: (716)789-1234


Figure 7-4: The fgetcsv() Output

Reading a File Using the PHP fgets() Function

You can use the fgets() function to read a file one line at a time.

<?php
$fh = fopen("users.txt", "rt"); // r = read from the beginning; t = Windows translation mode
// Read and write each line of a file.
while (!feof($fh)) {
  echo(fgets($fh) . "<br/>");
}
fclose($fh);
?>

Figure 7-5: The fgets() Function
Ale ale@example.com
Nicole nicole@example.com
Laura laura@example.com

Figure 7-6: The fgets() Output

Reading a File Using the PHP file() Function

You can also use the file() function to read a file into an array without using file handle to open or close the file.

<?php
$users = file("users.txt");
foreach ($users as $user) {
  list($name, $email) = explode(" ", $user);
  // Remove newline from $email
  $email = trim($email);
  echo("<a href=\"mailto:$email\">$name</a><br/>\n");
}
?>

Figure 7-7: The file() Function
Ale
Nicole
Laura

Figure 7-8: The file() Output

Writing a File Using the PHP fwrite() Function

You can use the "a+" file mode on the fopen() function to append to a file. You can then use the fwrite() function to write to that file.

<?php
$fh = fopen("users.txt", "a+"); // open the file for append
echo("Adding $subscriberInfo to the end of the users.txt file");
fwrite($fh, "\n$subscriberInfo");
fclose($fh);
?>

Figure 7-9: The fwrite() Function
Adding Jim gerlanjr@buffalostate.edu to the end of the users.txt file

Figure 7-10: The fwrite() Output

Task - Create a Web Page That Reads and Writes a File

For this Assignment you will create a web page named io.php on your local web server. This web page should:

You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.

Let's Get Started with a PHP Input/Output Page!

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland