Search code examples
phpdirectory-structure

Create a directory if it don't exist according the string?


$page = '......';

$the_file = 'cache/filing/forums/index.tpl';

$file = fopen($the_file, "w");
fwrite($file, $page);
fclose($file);

I need assistance, as I'm unsure on how to proceed. I have the $the_file variable (which is a string), and I want to create that directory structure and file (if they don't already exist). I've tried using fwrite() however that only works if the (sub)?directory(s)? exist - what I'm looking for is a method so if the (sub)?directory(s)? do not exist for it them to be created it.

E.g. if the cache and filing folders existed...but the forums didn't I'd like for it to be created so the index.tpl file can be created and placed within it with the $page's content.

Hope that makes sense.


Solution

  • mkdir(dirname($the_file), 0777, true);