Advertisement
Google Ad Slot: content-top
PHP File Handling
PHP File Handling
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.
PHP File Manipulation
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:
- Data Loss: Overwriting or deleting important files.
PHP readfile() Function
The 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):
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.