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:

Hello, welcome to PHP readfile() tutorial!

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

<?php
// Read and display the content of the file
echo "File Content:<br>";
readfile("example.txt");
?>

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.


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.