1. Opening a file:
To open a file in PHP, you can use the `fopen()` function. This function takes two parameters – the name of the file and the mode in which to open the file (e.g., read, write, append).
Example:
“`php
$file = fopen(“example.txt”, “r”);
“`
2. Reading a file:
To read the contents of a file, you can use the `fread()` function. This function takes two parameters – the file handle returned by `fopen()` and the number of bytes to read.
Example:
“`php
$file = fopen(“example.txt”, “r”);
$content = fread($file, filesize(“example.txt”));
fclose($file);
“`
3. Writing to a file:
To write content to a file, you can use the `fwrite()` function. This function takes two parameters – the file handle returned by `fopen()` and the content to write.
Example:
“`php
$file = fopen(“example.txt”, “w”);
fwrite($file, “Hello, World!”);
fclose($file);
“`
4. Appending to a file:
To append content to an existing file, you can use the `fwrite()` function with the “a” mode parameter. This will move the file pointer to the end of the file before writing.
Example:
“`php
$file = fopen(“example.txt”, “a”);
fwrite($file, “This is additional content.”);
fclose($file);
“`
5. Deleting a file:
To delete a file, you can use the `unlink()` function. This function takes the name of the file as a parameter and permanently deletes it.
Example:
“`php
unlink(“example.txt”);
“`
Note: These are just basic examples of file operations in PHP. There are many more functions available for file manipulation, such as copying files, renaming files, and checking file existence.