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?
$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.
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.
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.
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).
Yes, you can pass the recursive
parameter as true
;
mkdir('images/user/15', 0777, true);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With