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.
Related
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);
?>
Have been researching this for a day and can't figure it out. I am able to successfully create a folder when a user signs in that assigns it the username from the registration using mkdir(). The problem is, that I have to sign up another user before the first user's folder is added to my hosting, so the latest user never has a folder added until another user registers.
I am starting a new company so just testing it out myself and working from the bottom up, so I know I need to add more security to what I have, but I want to make sure I get this figured out before I move on.
Everything I have researched online and at php.net doesn't note anything I would need to add (that I can find).
Any recommendations would be appreciated. Thank you for the help.
Here is the code I have.
<?php
$author=$_POST['username'];
$root = "upload";
mkdir("$root/$author", 0777, true);
?>
Thank you all again for the help.
Okay, try the following and post back.
<?php
$author = $_POST['username'];
$root = "upload";
$path = $root.'/'.$author;
mkdir($path, 0777, true);
clearstatcache();
if(file_exists($path)){
echo $path.' -> directory does exists';
} else {
echo $path.' -> directory does not exist';
}
?>
I'm new in DirectAdmin. I'm facing a problem that when user try to create a folder, they will get error, "Unable to upload data." (refer to my code).
I think my code should be no problem as it can run smoothly in Localhost. The problem rise when I run on live server (DirectAdmin).
$id = $this->session->userdata('id');
$directory = "./image/userFolder/" . $id;
if(!is_dir($directory)) {
mkdir($directory, 0777, true);
}
$directory = $directory . "/" . $nameImage;
if(!imagejpeg($big_image, $directory)) {
$data['error'] = "Unable to upload data.";
return $data;
}
Hope to get answer or maybe something that I can look after. Thank you.
I managed to solve the problem. Basically my folder's permission is 0755. Then I change to 0777 that enable user to make folder (mkdir) in the server. You can learn more about permission here.
I'm using below PHP code here to create a particular folder if it didn't exist. I'm using joomla 2.5
$path = my/path/goes/here;
$folder_permissions = "0755";
$folder_permissions = octdec((int)$folder_permissions);
//create folder if not exists
if (!JFolder::exists($path)){
JFolder::create($path, $folder_permissions);
}
But this code throws below error
JFolder::create: Could not create directory
What could be the reason for this?
2 things I think might be causing the problem:
You forgot a ; on the end of octdec((int)$folder_permissions)
Try removing the whole line $folder_permissions = octdec((int)$folder_permissions)
Update:
This is what I used to create a simple folder:
$destination = JPATH_SITE.'/'."modules/mod_login";
$folder_name = "new_folder";
JFolder::create($destination .'/'. $folder_name, 0755);
What could the reason be?
Simple: The user that tries to create that directory doesn't have permission (or your Joomla is broken).
The user that runs the code is probably www-data (on most *nix/Apache). ALso 'nobody' or 'apache' are possible.
If you have rootpermission try this:
1) become the webuser (eg www-data)
2) Jump to the directory my/path/goes/here
3) type: mkdir mynewdir
And you will probably find out that the user doesn't have sufficient permissions to do so.
edit your configuration.php file under Joomla home direcory, change:
From:
var $tmp_path = '/home/public_html/your_name/tmp';
To:
var $tmp_path = 'tmp';
and tmp should have 777 permission
I need to create a directory using php with write permission...currently am using the following code
$folder_name = $this->input->post('foldername', true);
$path = '/home/temp/workspace/My_folder/documents/'.$folder_name;
mkdir($path,'0222');
But this is not working...
Does your application (Apache probably) have the right to write in that directory? Does the directory exist?
try this maybe:
$path = '/home/temp/workspace/My_folder/documents/'.$folder_name;
mkdir($path, 0222, $recursive=true);