Basic
Advanced
Examples
PHP 8 - Files
Last modified: April 02, 2022Create a test.txt in "C:\Temp" with below content
The iPhone is a line of smartphones designed and marketed by Apple Inc. that use Apple's iOS mobile operating system. The first-generation iPhone was announced by then-Apple CEO Steve Jobs on January 9, 2007. Since then, Apple has annually released new iPhone models and iOS updates. As of November 1, 2018, more than 2.2 billion iPhones had been sold.
- file_exists() check if the file exists on given path
- readfile() read and write to output buffer
- fopen() you more options than the readfile()
- unlink() It delete the file
1. file_exists()
<?php
if (file_exists('C:\Temp\test.txt'))
{
echo "file found";
}
?>
output
file found
2. readfile()
<?php
echo readfile("C:\Temp\\test.txt");
?>
output
The iPhone is a line of smartphones designed and marketed by Apple Inc. that use Apple's iOS mobile operating system. The first-generation iPhone was announced by then-Apple CEO Steve Jobs on January 9, 2007. Since then, Apple has annually released new iPhone models and iOS updates. As of November 1, 2018, more than 2.2 billion iPhones had been sold.352
3. fopen()
<?php
$file = fopen("C:\Temp\\test.txt", "r"); //open the file in readonly mode
while(! feof($file)) {
$line = fgets($file); //read each line
echo $line. "<br>";
}
fclose($file); //close the connection
?>
Mode possible values
- "r" - Read only
- "r+" - Read/Write
- "w" - Write only
- "w+" - Read/Write
output
The iPhone is a line of smartphones designed and marketed by Apple Inc. that use Apple's iOS mobile operating system. The first-generation iPhone was announced by then-Apple CEO Steve Jobs on January 9, 2007. Since then, Apple has annually released new iPhone models and iOS updates. As of November 1, 2018, more than 2.2 billion iPhones had been sold.352
4. unlink()
<?php
if (unlink("C:\Temp\\test.txt"))
{
echo "File is deleted";
}
?>
File is deleted