+3 votes
in Programming Languages by (60.0k 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

+2 votes
by (353k points)
selected by
Best answer

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

Here is an example:

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