PHP File Handling


File management is an integral part of any web application. You usually need to open and process a file for various tasks.


PHP Manipulating Files

PHP has several functions for creating, reading, uploading, and editing files.


Be careful when copying files!

If you are copying files you should be very careful.

You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling the hard-drive with garbage data, and accidentally deleting file contents.


PHP readfile() Function

The readfile () function reads the file and writes it in the output bar.

Suppose we have a text file called "webdictionary.txt", which is stored on the server, which 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

PHP code to read the file and write it to the output database is as follows (readfile () function returns the number of successful read bits):


Example
<?php
echo readfile("webdictionary.txt");
?>

The readfile function () is useful if you just want to open a file and read its contents.

The following chapters will teach you more about file management.