PHP mkdir(); not working - php

I've been trying the function Mkdir that will be usefull in the project i'm working on. I've tried the simplest code possible but I can't get it to create the folder I want.
I've tried to changes my folder permissions, but that doesn't change (Nor 755 or 777) and the code keeps returning a fail.
Please have a look at my code :
<?php
if(!mkdir($_SERVER['DOCUMENT_ROOT'].'/uploads/2017', 0777, true))
{
echo("echec");
}
chmod($_SERVER['DOCUMENT_ROOT'].'/uploads/2017', 0777);
?>
The parent folder is "admin" and it permissions are set to 755.
Do you have any clue why this isn't working ?
EDIT : I remade it and it worked, no clue what the problem was.

Code
mkdir('/2017', 0777, true)
creates folder 2017 is a root folder of a file system.
Always set ethier full path to your folder, e.g.:
mkdir($_SERVER['DOCUMENT_ROOT'] . '/2017', 0777, true);
// or
mkdir('/var/www/mysite/2017', 0777, true);
Or use . or .. to define proper location:
// folder will be created in a same directory
// as a script which executes this code
mkdir('./2017', 0777, true);
// folder will be created in a directory up one level
// than a script which executes this code
mkdir('../2017', 0777, true);
So, in your case it is obviously:
mkdir($_SERVER['DOCUMENT_ROOT'] . '/admin/2017', 0777, true);

Example #1 mkdir() example
<?php
mkdir("/path/to/my/dir", 0700);
?>

Related

To Create Multiple Directories using mkdir() in Php

I am working on a social network website, in which multiple directories are created on signup. Here is the code I am using:
$path = "fb_users/Organization/" . $user . "/Profile/";
$path2 = "fb_users/Organization/" . $user . "/Post/";
$path3 = "fb_users/Organization/" . $user . "/Cover/";
mkdir($path, 0, true);
mkdir($path2, 0, true);
mkdir($path3, 0, true);
The code is working good on my localhost, but when I am using the same code on cPanel hosting, the code only creates fb_users/Organization/fb#abc.com (let $user = fb#abc.com). It's not creating three more folders. Will anyone please get me out of this?
Directory before code:
/fb_users/Organization
After running code on localhost:
/fb_users/Organization/fb#abc.com/Cover
/fb_users/Organization/fb#abc.com/Post
/fb_users/Organization/fb#abc.com/Profile
Same code running on hosting using cPanel:
/fb_users/Organization/fb#abc.com (only this directory is created)
When you specify mode = 0 on a Unix server, it will create the top-level directory /fb_users/Organization/fb#abc.com with no read or write permissions, even for the owner. So it won't have permission to create the subdirectories. Use 0700 to give the owner full permissions, but not allow anyone else to access it.
mkdir($path, 0700, true);
mkdir($path2, 0700, true);
mkdir($path3, 0700, true);
Try changing the second parameter to 0700.
Edit: Barmar beat me to it, with a better explanation.

CodeIgniter zip archive with exclude a folder

I'm trying to create full site backup but i want to exclude a folder from archive.
My directory:
/application/
/backups/ >>> I dont want to archive this folder because of nested archiving.
/themes/
/uploads/
.htaccess
index.php
Tried following codes:
$this->load->library('zip');
$this->zip->read_dir(FCPATH);
$this->zip->archive(FCPATH.'backups/'.date('Y-m-d-His').'.zip');
You can workout something like this, in which you will never have to manually enter the new directory you create
$this->load->library('zip');
$data = array_diff(scandir(FCPATH), array('..', '.','backups'));
// 'backups' folder will be excluded here with '.' and '..'
foreach($data as $d) {
$path = FCPATH.$d;
if(is_dir($path))
$this->zip->read_dir($path, false);
if(is_file($path))
$this->zip->read_file($path, false);
}
$this->zip->archive(FCPATH.'backups/'.date('Y-m-d-His').'.zip');
My solution is a little manual that setting each root directories and files like following:
$this->load->library('zip');
// Choose directory and files
$this->zip->read_dir(FCPATH.'application', false);
$this->zip->read_dir(FCPATH.'themes', false);
$this->zip->read_dir(FCPATH.'uploads', false);
$this->zip->read_file(FCPATH.'.htaccess', false);
$this->zip->read_file(FCPATH.'index.php', false);
$this->zip->archive(FCPATH.'backups/'.date('Y-m-d-His').'.zip');
Tested on CodeIgniter 3.x and Wamp Server

create directory in parent root with php in laravel

my site is in /var/www/mysite directory .i use laravel and want to create directory in /uploads directory .so /uploads is besides of /var .
/var/www/mysite
/uploads
i set 0777 permission for /uploads but permission denied happened.
I try with this code
<?php mkdir('/uploads', 0777 , true) ; ?>
Your web user (www-data, apache or whichever) is restricted by the operating system to use /var/www. You can't read,write,delete any files outside /var/www. So that's not a laravel issue.
Function that create all directories (folders) of given path. No need to write code create each directories (folders) of given path. it will create all directories (folders).
Like : If you want to create directory structure like
organizations / 1 / users / 1 /
So you only need to call this function with directories path like
$directories_path = 'organizations/1/users/1/';
createUploadDirectories($directories_path);
/*
* Method Name : createUploadDirectories
* Parameter : null
* Task : Loading view for create directries for upload
*/
if ( ! function_exists('createUploadDirectories')){
function createUploadDirectories($upload_path=null){
if($upload_path==null) return false;
$upload_directories = explode('/',$upload_path);
$createDirectory = array();
foreach ($upload_directories as $upload_directory){
$createDirectory[] = $upload_directory;
$createDirectoryPath = implode('/',$createDirectory);
if(!is_dir($createDirectoryPath)){
$old = umask(0);
mkdir($createDirectoryPath,0777);// Create the folde if not exist and give permission
umask($old);
}
}
return true;
}
}
Do this:
Use File;
File::makeDirectory(base_path() . '/path/to/directory', 0775, true);
This will attempt to create /path if it doesn't exist. Then /path/to if it doesn't exist. And finally /path/to/directory if it doesn't exist. If all created directories are successfully created then true will be returned.
If you want to place the directory in your public path; use:
File::makeDirectory(public_path() . '/path/to/directory', 0775, true);
Source: Laravel Recipes

Php: creating directory

I am trying to create folder and sub folder on website. Code is pretty simple. Not sure why does not work.
<?php
$domain = "officeactionuspto.com";
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain), 0777, true);
mkdir(($_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images'), 0777, true);
$folder= $_SERVER["DOCUMENT_ROOT"].'/crc/website_templates/client_files/'.$domain.'/images';
if(is_dir($folder))
{
echo ("$folder is a directory");
}
else
{
echo ("$folder is not a directory");
}
?>
You don't have to use absolute path to create a directory.
You can just do it with following code:
mkdir('images', 0777, true);
$folder= 'images';
if(is_dir($folder)){
echo ("$folder is a directory");
}else{
echo ("$folder is not a directory");
}
you can also get the absolute path after created, if you desired:
$full_path = realpath('images');
PS: I'm supposing you are executing this code on /index.php, if was on another different structure, you need to write the relative path for it.
EDIT: I tested and eliminate a parentheses on mkdir and works.
You don't need to use the entire, absolute filename. You just need to use the path relative to the folder where the script being executed is located.
Although I don't know your file structure, lets pretend that the PHP script is in the crc folder: Your command would be: mkdir(('/website_templates/client_files/'.$domain), 0777, true);
EDIT: With the recursive parameter, you can create the images subfolder in the same command.

mkdir() in php is setting folder permission to 755 But I Need 777?

I am trying to create a folder on my server using php when i set it to 0777 it comes out as 755?
mkdir($create_path, 0777);
Thank you
Try this:
$old_umask = umask(0);
mkdir($create_path, 0777);
umask($old_umask);
http://php.net/umask
This really works for me!, you should close now this question!
Create the directory!
Give 777 permissions!
$estructure = '../files/folderName';
if(!mkdir($estructure, 0777, true)){
echo "<br/><br/>ERROR: Fail to create the folder...<br/><br/>";
} else echo "<br/><br/>!! Folder Created...<br/><br/>";
chmod($estructure, 0777);
Enjoy it!
Try this:
<?php
// files will create as -rw-------
umask(0);
// create a file, eg fopen()
chmod('/path/to/directory', 0777);
?>
Reference
The umask of the process is set to 0022. You'll need to set it to 0 if you want to create something with those two write bits set.

Categories