PHP Basic Tutorial
MySQL Connection
PHP Advanced
File handling in PHP allows you to create, read, write, and manage files on the server. PHP provides built-in functions to perform these operations effectively.
In PHP, file manipulation involves operations like creating, reading, writing, appending, renaming, copying, and deleting files.
Warning
Be Careful When Manipulating Files in PHP
File manipulation in PHP can be risky if not handled properly. Careless file operations can lead to:
readfile()
FunctionThe readfile()
function in PHP is used to read and output the contents of a file directly to the browser. It’s a simple way to display the content of files like .txt
, .html
, or other plain text files.
Assume we have a text file called "example.txt", stored on the server, that looks like this:
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):
Try it yourself
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.