[英]PHP create multiple directories
Let's say, in PHP, I am trying to put an image in a specific directory that is on root. 让我们说,在PHP中,我试图将图像放在root上的特定目录中。
I want to put it on /images/afc/esp/stadium/
directory. 我想把它放在
/images/afc/esp/stadium/
目录下。 Images folder, Federation folder, Country ISO3 folder, content folder. 图像文件夹,联合文件夹,国家ISO3文件夹,内容文件夹。
$folder_full = "images/".$getFed."/".$country_folder."/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full);
Before you ask, yes $getFed
and $country_folder
work and output text. 在你问之前,是
$getFed
和$country_folder
工作和输出文本。 So, then I get this error: Warning: mkdir(): No such file or directory
所以,我得到这个错误:
Warning: mkdir(): No such file or directory
I don't get it? 我不明白吗?
Some of your subdirectories don't exist so you either need to iteratively create them or set the 3rd argument to mkdir()
to true
. 您的某些子目录不存在,因此您需要迭代创建它们或将
mkdir()
的第3个参数设置为true
。 Note that the second argument are the directory permissions (ignored on Windows) which default to 0777
. 请注意,第二个参数是目录权限(在Windows上被忽略),默认为
0777
。
You also need to set $folder_full
to the root using /
. 您还需要使用
/
将$folder_full
设置$folder_full
root。
$folder_full = "/images/{$getFed}/{$country_folder}/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full, 0777, true);
All the intermediary directories have to already exist. 所有中间目录必须已经存在。 You can trigger this behaviour by using the optional third argument:
您可以使用可选的第三个参数触发此行为:
mkdir($folder_full,0777,true);
在正常的cPanel中,文件夹权限应为0755,因此本例中的命令将为:
mkdir('tst/tst2/tst3', 0755, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.