Download presentation
Presentation is loading. Please wait.
1
Website Development Tutorial 3
2
Outline Getting data from the user using forms Sending data from a form to a PHP program Writing the data to a file for permanent storage Reading data back from a file
3
File reading and writing Useful for data sets too small for a database Enables code to read and write to a file Typical session might follow these steps Open the file for read/write Read in the contents Close the file (if only reading data) Perform operations of the file contents Write the results Close the file W3schools Tutorial
4
The fopen() function is used to open files in PHP. <?php $file=fopen(“guestbook.txt","r"); ?>
5
If you can’t open the file <?php $file=fopen(“guestbook.txt","r") or exit("Unable to open file!"); ?>
6
Closing the file <?php $file = fopen(“guestbook.txt","r"); //some code to be executed fclose($file); ?>
7
read a file line by line, until the end of file is reached: <?php $file = fopen(“guestbook.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). " "; } fclose($file); ?>
9
Any writing to a file is done with the use of “fwrite()” or “fputs()” functions, which both use a file pointer and a string to perform the writing:
10
General Example $fd = fopen($filename, “r+”) or die(“Can’t open file $filename”); $fstring = fread($fd, filesize($filename)); $fout = fwrite($fd,$fstring); fclose($fd);
11
File open fopen() Have to assign the result to a variable Usually $fd (file descriptor) or $fp (file pointer) fopen() doesn’t return anything useful if unsuccessful (from a test point of view) so use die() to test for failure Six modes for opening “r” – read only “r+” – read and write if the file exists already “w” – write only “w+” – write and read even if the file doesn’t exist “a” – write only to the end of the file “a+” – read and write to the end of the file
12
File read fread() Takes a file-pointer and file size (in bytes) Most common way to send size is to use the filesize() function $fstring = fread($fd, filesize($filename)); Notice it is filesize($filename), not filesize($fd) – a common mistake Useful for turning a file’s contents into a string, can then use all the string functions to slice and dice the string Can also send the information to an array through use of file() and explode() functions – more later on these
13
File write Used for writing a string to a file Expects a file pointer and a string Returns the number of characters saved $fout = fwrite($fp, $string); if ($fout != strlen($fstring)) { echo “file write failed”; }
14
Important note on writing! If we use modes “w” and “w+” we will overwrite all the data in the file Which means that, EVERYTHING WILL BE LOST! If you want to add to beginning of file use “r+” If you want to add to end of file use “a+”
15
File close Does what it says on the tin fclose($fd); Not essential, PHP close all files at end of script, but its good house keeping
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.