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.
Reading a CSV File Using the PHP fgetcsv()
Function
You can use the fgetcsv()
function to read a file one line at a time.
Reading a File Using the PHP fgets() Function
You can use the fgets()
function to read a file one line at a time.
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.
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.
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:
- Use appropriate HTML5, CSS-styled tags that identify this as your web page including your name and a brief paragraph describing how you approached solving this problem.
- Use PHP to generate the following HTML:
- An appropriate
<!doctype html>
tag indicating that this is an HTML5 web page. - Appropriate
<html>
,<head>
, and<title>
tags to give your page a title that contains your name. Appropriate<meta>
tags with content describing keywords for the author, text-editor used, and SEO information. Use your vars.php file.
- An appropriate
- Use PHP to:
- Use appropriate PHP comments throughout your code.
- Add a function to your
functions.php
file that reads the text filecda215_io.txt
. - This function should use PHP array to store records (each line is a "record").
- This function should use a PHP Loop to walk through the months names array and generate HTML5/CSS to display the table.
- This function should use a PHP conditional (
if
) test to (use the mod (%
) operator) determine whether the row is odd or even and use appropriate CSS to make the even rows a different background/foreground color than the odd rows. - Create a function that writes the text file
cda215_ior.txt
(reverse order). - This function should use PHP
for
loop to walk through the array in reverse order and write each array entry (line/record) to thecda215_ior.txt
file.
- Appropriate HTML5, CSS-styled tags that identify this as your web page including your name and a brief paragraph describing how you approached solving this problem.
- You should use appropriately identified and styled
<div>
tags for the various areas of your page layout. - Modify your JavaScript in your index.html file that uses an array of the Assignments for this course to display an ordered list of these assignments to convert the entry for this assignment to a web link that allows me download and save your files and grade your work.
You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.