Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn PHP File Handling



PHP File Handling

PHP provides several functions for handling files, such as reading from, writing to, and manipulating files. Here's an example of how you can create an HTML file and a PHP file that interacts with it:



PHP Manipulating Files

PHP has several functions for creating, reading, uploading, and editing files.


Be careful when manipulating files!
When you are manipulating files you must be very careful.
You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.



PHP readfile() Function

The readfile() function reads a file and writes it to the output buffer.

Assume we have a text file called "webdictionary.txt", stored on the server, that 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 write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):


example

<?php
echo readfile("webdictionary.txt");
?>


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.


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java