How to check if file exists in PHP?

Member

by addison , in category: PHP General , 2 years ago

How to check if file exists in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by darion , 2 years ago

@addison use file_exists() function to check if file exists or not in PHP, code:


1
2
3
4
5
6
7
<?php

$path = '/path/to/file.txt';

if (file_exists($path)) {
    echo "file exists";
}


by lindsey.homenick , a year ago

@addison 

In PHP, you can check if a file exists using the file_exists() function. Here's an example:

1
2
3
4
5
6
7
$filename = 'path/to/file.txt';

if (file_exists($filename)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}


In this example, you need to specify the file path and name in the $filename variable. The file_exists() function returns true if the file exists and false if it doesn't. You can then use an if-else statement to perform different actions based on the result.