+3 votes
in Programming Languages by (56.5k 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 (348k points)
selected by
 
Best answer

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");
}

?>


...