+2 votes
in Programming Languages by (6.4k points)
One of my codes generates files for the another code. Before reading the file, I want to check whether or not the file has been generated.

How can I check if the file exists in the folder?

1 Answer

0 votes
by (26.1k points)

You can use the file_exists() function to check whether a file or directory exists.

Here is an example:

<?php
$file = "abc1.csv";
$dirname = "/home/user/php";
//if (file_exists($file)){
if (file_exists($dirname)){
    print("File exists\n");
}else{
    print("File does not exist\n");
}

?>

...