Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell php to create a directory and another directory inside that one

Tags:

php

How do I tell php to create a directory and then another directory inside that directory?

I'm using mkdir here. I have a folder called images. I need to create a folder inside images called 'user', then a folder under user called '15'. I can create the folder called user in one go. How can I do both together?

like image 533
Norman Avatar asked Oct 02 '12 14:10

Norman


People also ask

How can I create a folder within a folder in PHP?

$the_path = '/user/15'; $the_mode = '0700'; mkdir($the_path,$the_mode, true); you can generate the required path & permissions for the new directory, pass them to the mkdir function along with setting the 'recursive' flag to true. Save this answer.

Which function is used for creating a directory in PHP?

The mkdir() function is used to create directory in PHP. It is an inbuilt function in PHP. The mkdir() function creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure.

How do I move a directory to another directory in PHP?

If you need to move file from one folder to another using php code then we can use "rename()" function of php. php provide rename function to move your file from one place to another.

How do I open a file in another directory in PHP?

To access the root begin your path with "/". If you want to go up one directory from where you are currently, e.g. from /x/a/ to /x/ you could use "../". If you want to go back up two directories (e.g. from /x/a/ to /) you could use "../../" (rather than ".../" which you mentioned).


2 Answers

Yes, you can pass the recursive parameter as true;

mkdir('images/user/15', 0777, true);
like image 119
Björn Avatar answered Oct 21 '22 06:10

Björn


Use the recursive flag of mkdir function

The function signature is:

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

So use it like so

mkdir('images/user/15',0777,true);

Though it is also advisable not to use 777 mode, but that is another matter.

like image 45
DeadAlready Avatar answered Oct 21 '22 04:10

DeadAlready