如果根据字符串不存在则创建一个目录?

发布于 2024-10-07 01:38:36 字数 446 浏览 0 评论 0原文

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

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

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

我需要帮助,因为我不确定如何继续。我有 $the_file 变量(它是一个字符串),我想创建该目录结构和文件(如果它们尚不存在)。我尝试过使用 fwrite() 但只有在 (sub)?directory(s)? 时才有效。存在 - 我正在寻找的是一种方法,所以如果(子)?目录?它不存在,因此他们无法创建它。

例如,如果缓存和归档文件夹存在...但论坛不存在,我希望创建它,以便可以创建 index.tpl 文件并将其与 $page 一起放置在其中的内容。

希望这是有道理的。

$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-10-14 01:38:36
mkdir(dirname($the_file), 0777, true);
mkdir(dirname($the_file), 0777, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文