Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
Kishor Kumar Avatar asked Dec 13 '11 06:12

Kishor Kumar


People also ask

How to create a multilevel category in PHP using MySQL?

You must connect PHP to the MySQL database with its credentials. After that start writing code for creating a multilevel category File Name – database.php 3. Create a Multilevel category Using PHP To create a multilevel category, you have to declare the following PHP functions –

What are multidimensional arrays in PHP?

For this, we have multidimensional arrays. A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

How to use multiple namespaces in one PHP file?

Each PHP file can belong to a single namespace, and you tell that by writing the following code at the beginning. You can also nest namespaces into one another, like namespace MyNamespace\SubNamespace. Then, you can use the same class names in different namespaces, they will still remain different classes with no conflict.

What is the best way to structure a PHP project?

With this PHP project structure, your PHP application will be much more maintainable. One additional thing, not strictly related to the PHP project structure, is the naming convention. You can have snake casing, camel casing and so on. You can put curly brackets on the same line of the if, or on the next line.


2 Answers

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.

like image 148
Indranil Avatar answered Sep 22 '22 16:09

Indranil


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

like image 43
Jared Farrish Avatar answered Sep 21 '22 16:09

Jared Farrish