Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP File Handling

PHP Manipulating Files

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.

PHP readfile() Function

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):

Example

<?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.