File handling is an important part of any web application. You often need to open and process a file for different tasks.
PHP Manipulating Files
PHP has several functions for creating, reading, uploading, and editing files.
Be careful when manipulating files!
When you are manipulating files you must be very careful.You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.
PHP readfile() Function
The readfile()
function reads a file and writes it to the output buffer.
Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The PHP code to read the file and write it to the output buffer is as follows (the readfile()
function returns the number of bytes read on success):
Example
<?php
echo readfile("webdictionary.txt");
?>
The readfile()
function is useful if all you want to do is open up a file and read its contents.
The next chapters will teach you more about file handling.
PHP Open File - fopen()
A better method to open files is with the fopen()
function. This function gives you more options than the readfile()
function.
We will use the text file, "webdictionary.txt", during the lessons:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The first parameter of fopen()
contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:
Example
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Tip: The fread()
and the fclose()
functions will be explained below.
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already existsPHP Read File - fread()The The first parameter of The following PHP code reads the "webdictionary.txt" file to the end:
PHP Close File - fclose()The It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources! The
PHP Read Single Line - fgets()The The example below outputs the first line of the "webdictionary.txt" file: Example<?php Note: After a call to the PHP Check End-Of-File - feof()The The The example below reads the "webdictionary.txt" file line by line, until end-of-file is reached: Example<?php PHP Read Single Character - fgetc()The The example below reads the "webdictionary.txt" file character by character, until end-of-file is reached: Example<?php PHP Create File - fopen()The If you use The example below creates a new file called "testfile.txt". The file will be created in the same directory where the PHP code resides: Example$myfile = fopen("testfile.txt", "w") PHP File PermissionsIf you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive. PHP Write to File - fwrite()The The first parameter of The example below writes a couple of names into a new file called "newfile.txt": Example<?php PHP OverwritingNow that "newfile.txt" contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file. In the example below we open our existing file "newfile.txt", and write some new data into it: Example<?php If we now open the "newfile.txt" file, both John and Jane have vanished, and only the data we just wrote is present:
Notice that we wrote to the file "newfile.txt" twice. Each time we wrote to the file we sent the string $txt that first contained "John Doe" and second contained "Jane Doe". After we finished writing, we closed the file using the If we open the "newfile.txt" file it would look like this:
|
0 Comments