Creating a layered subfolder structure in PHP

I am trying to create a folder structure that has multi-level subfolders. For example, I want to create a folder structure, for example "Fruits / Edible / Seedless". I tried this with mkdir ($ path), but this failed. I tried with a folder of the same level, created it. Help me create this subfolder structure.

+4
source share
3 answers

Try using a recursive flag for mkdir ($ path, $ chmod, $ recursive)

<?php mkdir($path, 0, true); ?> 

From php.net = recursive Allows you to create subdirectories specified in the path name. The default is FALSE.

+13
source
 bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] ) 

See specifically: bool $recursive = false .

http://php.net/manual/en/function.mkdir.php

+6
source

You can also use the Linux exec command as follows to achieve this,

 <?php exec("mkdir -p ".$path); ?> 

-p will not cause errors if the directory exists, otherwise it will create the directory along with the parent directories.

0
source

Source: https://habr.com/ru/post/1386044/


All Articles