cakephp - 在webroot中创建新目录

I want to create a new directory(folder) inside the webroot so i can create/edit files inside that new folder.

My code is:

$path = 'files' . DS . 'pathtofolder';// $folder_name;
$folder = new Folder($path);
if (!is_null($folder->path)) { //this is to verify if created
    echo "Exists";
}
else{
    echo "Doesnt exist";
}

The result is always doesnt exist. And i cant find any created folder inside my cakephp folders.

I tried to change 'files' for 'webroot' but it doesnt work.

Is this the correct code to create a directory?

try this

$path = 'files' . DS . 'pathtofolder';
$folder = new Folder($path, true, 0755);
if ($folder->path) { 
    echo "Exists";
}
else{
    echo "Doesnt exist";
}

There is a direct PHP call to create directories. So, you can use that. I dont know the cakephp version.

if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}

Also, check if your web-server user www-data has permissions to create directories. Otherwise, on command prompt run this command

addgroup www-data

That will add www-data to enable permissions. You might need to add sudo in some cases to above command if you get permission denied error.

$path = WWW_ROOT . 'webroot' . DS . 'FolderNameYouWantToSet' . DS;
                if (!file_exists($path)) {
                    $oldMask = umask(0);
                    mkdir($path, 0777, true);
                    chmod($path, 0777);
                    umask($oldMask);
                }

This will create a folder with 777 permission, you can set the folder permission as per your requirement.

WWW_ROOT is the global cake variable which stores the path up to the project.