PHP offers several functions for creating, reading, uploading, and modifying files.
|
Be cautious when working with files! File manipulation requires careful attention. Mistakes can cause significant issues, such as editing the wrong file, filling a hard drive with unwanted data, or accidentally deleting file content. |
The readfile() function reads a file and outputs its content directly to the output buffer.
Assume we have a text file named “webdictionary.txt” 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 |
The PHP code to read the file and output its content is as follows (the readfile() function returns the number of bytes read upon success):
| <?php echo readfile(“webdictionary.txt”); ?> |
The readfile() function is handy if you simply need to open a file and read its contents.
The upcoming chapters will cover more details about file handling.