How to check if a folder or a file exists and build a folder with PHP

This is an example to check if a folder exists and if not exist, build this folder:
//produce the string according to the date and the format is like this: 201010
$subfolder = gmdate(“Ym”, time());

//file_exists() check if the folder or a file exists; if it exists, reture true, otherwise false.
// dirname(__FILE__) returns the current folder path.
$dir = dirname(__FILE__) . $subfolder;
if (file_exists($dir)) {
echo “this folder” . $dir . “exits”;
} else {
mkdir($dir,0777);
}
?>

An example to check if a file exists under the folder:
$dir = dirname(__FILE__) . “\\201010”;
$file = $dir . “\\data.txt”;

if (file_exists($file)) {
echo “In the folder: ” . $dir . “, the file ” . $file . “exits”;
} else {
echo “In the folder” . $dir . “,” . $file . “doesn’t exist”;
}
?>

I found that the folders that are created by the script above on the web hosting space and the files in them can’t be deleted through the interface provided by cPanel.

Leave a Reply

Your email address will not be published. Required fields are marked *