Kishor Kumar
Kishor Kumar

Reputation: 543

Create multi level sub folder structure in PHP

I'm trying to create a folder structure which has multi level sub folders. For example, I want to create a folder structure like 'Fruits/Edible/Seedless'. I tried it with mkdir($path) but it could not done. I tried with single level folder, its created. Help me to create this subfolder structure.

Upvotes: 5

Views: 8688

Answers (3)

Indranil
Indranil

Reputation: 2471

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

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

From php.net= recursive Allows the creation of nested directories specified in the pathname. Defaults to FALSE.

Upvotes: 14

Stranger
Stranger

Reputation: 10621

You can also use the Linux exec command in the following way to achieve this,

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

-p won't throw any error if directory exists, otherwise it will create the directory along with the parent directories.

Upvotes: 0

Jared Farrish
Jared Farrish

Reputation: 49218

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

Upvotes: 6

Related Questions