I have to create a root folder that uses the $username
variable (coming from a previous section of the script), and then, inside this root folder, I need to create the /JS
, /PHP
and the /CSS
subfolders.
I have tried the following (just for the JS subfolder) but it does not work. I receive no error but the JS subfolder is not created (the root folder is created):
$rootpath = $username. '/';
$jspath = $rootpath. '/js/';
return is_dir($rootpath) || mkdir($rootpath, 0777, true);
return is_dir($jspath) || mkdir($jspath);
What am i doing wrong?
Thank you in advance.
Kindest regards,
My Answer:
You are close, but there is a mistake in your code. The issue is with the second return statement. When you use return, the function will exit and the subsequent code will not be executed. Instead, you should use an if statement to check if the directory exists and create it if it doesn't. Here is the corrected code:
php
$rootpath = $username. '/';
$jspath = $rootpath. 'js/';
if (!is_dir($rootpath)) {
mkdir($rootpath, 0777, true);
}
if (!is_dir($jspath)) {
mkdir($jspath, 0777, true);
}
This code will first check if the root folder exists, and if not, it will create it. Then it will check if the JS subfolder exists, and if not, it will create it. This way, both the root folder and the subfolder will be created successfully.
Rate this post
4 of 5 based on 9617 votesComments