cakephp - create new directory inside webroot - php

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.

Related

Php makedir in the same root folder

Please am using this to create a new directory, but is invalid path when i want to create folder in the same directory and is showing Invalid path specified. But if i try to create in a different folder it will work.
<?php
if(isset($_POST['makejail'])){
$jailorgname = $_POST['jailName'];
$parentDir = '/';//cjl I want to create in the same root folder
if(!is_dir($parentDir)) {
echo('Invalid path specified');
}
else if(!is_writable($parentDir)) {
echo('Unable to create directory, permissions denied.');
}
else if(file_exists($parentDir."/".$jailorgname)){
echo('Folder already exist');
}
else if(mkdir($parentDir."/".$jailorgname) === false) {
echo('Problems creating directory.');
}
else{
echo "Folder was created";
}
}?>
You're defining $parentDir wrong:
$parentDir = '/';
This points to the root directory of the machine, not the parent directory of your script.
You may want to use the magic constant __DIR__ like this:
$parentDir = __DIR__ . "/.."; // Go up one level from the current directory
See here for more info:
http://php.net/manual/en/language.constants.predefined.php

PHP creating a folder with the right path

<?php
if (isset($_POST['filename']) && isset($_POST['editorpassword']) && isset($_POST['roomname'])) {
$dir = $_POST['filename']; // This must match the "name" of your input
$path = "evo/" . $dir;
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
}
?>
I have this script where I'm trying to create a new folder. The script itself is ran inside of a folder called /evo and by using this code, it creates the folder in there. Where it needs to go is ../../creative however even if I try and use
$path = "./rooms/creative/" . $dir;
or something to that effect it creates it with the base folder as evo so it appears at:
../evo/rooms/creative (creating the folders that don't exist there with it as it should)
I'm just unsure what to write in for the path on where I need it created to find the right location.
Simplest solution is to remove the "evo" in $path = "evo/" . $dir;

PHP Create dir fails

i have ran into bit of a problem, i want to create a temp dir for some image uploads.
here is what i have:
if i run root#dev:/var/www/media# tree draft/ it returns
draft/ [error opening dir]
the dir is not there (and thats correct), so i want php to create the dir
then i use this:
if(!is_dir(MEDIA_PATH . "draft")){
create_dir(MEDIA_PATH . "draft");
}
if(!is_dir(MEDIA_PATH . "draft/product")){
create_dir(MEDIA_PATH . "draft/product");
}
if(!is_dir(MEDIA_PATH . "draft/product/" . $temp_id)){
create_dir(MEDIA_PATH . "draft/product/" . $temp_id);
}
function create_dir($file){
echo "create_dir: {$file}" . PHP_EOL;
$dir = dirname($file);
if(!is_dir($dir)){
umask(0000);
if(false === mkdir($dir, 0777, true) && !is_dir($dir)){
throw new RuntimeException(sprintf("Unable to create the directory (%s).", $dir));
}
umask(0022);
}elseif(!is_writable($dir)){
throw new RuntimeException(sprintf("Unable to write in the directory (%s).", $dir));
}
}
(https://gist.github.com/keja/840291d1abb7355f07aa)
that results in
create_dir: /var/www/media/draft
create_dir: /var/www/media/draft/product
create_dir: /var/www/media/draft/product/56d038fe-0906-9442-c2d7-ab2b841467af
then if i run the tree command again
root#dev:/var/www/media# tree draft/
draft/
└── product
for some reason it will not create the last dir, any ideas?
the /var/www/media is a NFS mount, but should not have anything todo with the problem i guess.
EDIT: Problem is solved.
the solution was to remove the $dir = dirname($filename); and change the $file param to $dir instead.
function create_dir($dir){
if(!is_dir($dir)){
umask(0000);
if(false === mkdir($dir, 0777, true) && !is_dir($dir)){
throw new RuntimeException(sprintf("Unable to create the directory (%s).", $dir));
}
umask(0022);
}elseif(!is_writable($dir)){
throw new RuntimeException(sprintf("Unable to write in the directory (%s).", $dir));
}
}

Php mkdir() is not creating a directory in my web directory

I can't make a new directory in my web server. I think my code is ok to create a directory. Can you tell me what is the error ?
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
mkdir($path);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
It's always showing me "directory is not created".
This is not just a permission issue, you are doing impossible things.
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
You can never use a url as the path to create a directory in a server.
The path should be the path in your web server like:
$path = "/path/to/your/project/astuces/uploads/products/".$id;
And then make sure the apache user has the permission.
If the the parent directory also not exist at first, you have to set the third parameter to true of mkdir:
if(mkdir($path, 0755, true)){
Try this code
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
$old_umask = umask(0);
mkdir($path, true);
chmod($path, 0777);
umask($old_umask);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}

How to create a folder in site->default->files with write read permissions with Drupal?

I'm working on Ubuntu and I need to create a folder in Drupal's default->files folder with write permission so that I would be able to add files to that folder later on.
Here is the code I have:
drupal_mkdir('public://' . $new_dir . '/');
$file = file_copy($file, 'public://' . $new_dir . '/' . $file_name);
drupal_mkdir('public://' . $new_dir , 0777);
if you need it to be recursive set third argument to true.
update:
$oldumask = umask(0);
drupal_mkdir("public://". $new_dir , 0777);
umask($oldumask);
fire this command and than try your code ..
chmod -R 0700 sites/default/files

Categories