You are on page 1of 8

F5224 WEB PROGRAMMING

FILE HANDLING WITH PHP

Displaying directory files


Special PHP functions can be used to display a list of all the files contained within any directory on your system. It must be opened with opendir() function Once open, a loop can assign all the file names to a variable list using readdir() function It is important to remember to close the directory when the loop has completed using closedir() function

Copying & renaming files


Files on your system can be copied to any location using PHPs copy() function that required two arguments: the full path of the source file to be copied, and the full path of the desired location where the copy is to be placed. You can also renamed files on your system using PHPs rename() function that required two arguments: the original name of the file and the new name which it will be changed.

Deleting files
The PHP unlink() function permanently deletes the files specified as its argument (if it is a valid file name)

Opening and Closing files


The PHP fopen() function is used to read text from files, write text to files and append text to files. It required two arguments: the file name and a mode in which to operate.

File modes can be specified as one of the six options:


Mode r r+ w Purpose Opens the file for reading only. Places the file pointer at the beginning of the files. Opens the file for reading and writing. Places the file pointer at the beginning of the file. Opens the file for writing only. Places the file pointer at the beginning of the file and truncates the file to zero length. If the file does not exist this will attempt to create it. Opens the file for reading and writing. Places the file pointer at the beginning of the file and truncates the file to zero length. If the file does not exist this will attempt to create it. Opens the file for writing only. Places the file pointer at the end of the file. If the file does not exist this will attempt to create it. Opens the file for reading and writing. Places the file pointer at the end of the file. If the file does not exist this will attempt to create it.

w+

a+

Reading a file
A file can be opened with the fopen() function A file can then be read with a function called fread() A files length can be found using the filesize() function fclose() function used to close file.

THE END