PHP mkdir permissions - wrong path - php

I have an problem and hope you can give me a hint for that:
I have an XAMPP running under OSx.
My APP is in htdocs/app.
Inside /app there is a folder /scripts and there is my PHP File:
app/scripts/file.php
Inside the /app folder there is a second folder /stuff that have 777 permissions.
from the script in app/scripts/file.php I want to create a folder
in app/stuff
But I got permission denied when I try to create a folder like:
mkdir('../scripts/newfolder', 0777, true);
What do I have to do?
Note:
When I test like this:
mkdir('newfolder', 0777, true);
Then it will work, but the new folder is under the wrong location:
app/scripts/newfolder

Give directory path as follows inside file.php.
mkdir('../stuff/newfolder', 0777, true);

If I understand you correctly you want to make a folder in app/stuff from the app/scripts folder, but you are telling the computer to go back into the apps folder (..), then back into the scripts folder (../scripts/) and then to make the newfolder.
mkdir('../stuff/newfolder', 0777, true);
should work then.

Your trying to create a directory from a dynamic place. You should prefer to use DIR which is the absolute directory of the current file being processed.
so you would use
mkdir(__DIR__ . '/../stuff/newfolder', 0777, true);
assume this line from your question is correct
from the script in app/scripts/file.php I want to create a folder in
app/stuff

Related

mkdir() not creating directory on web server even full path is specified

Please somebody help me, I want to create a folder 'uploads' in 'httpdocs' where my index.php file is placed, I am using Windows hosting with Plesk on Goddady. I went through available content on web but couldn't fix the issue, I am not very good with web servers. I have tried many solutions like full path specification, read - write permission, recursive directory creation using true/false etc but didn't work. It is working on my local server but not on web server. - Thanks in advance.
$path ="/PleskVhosts/abccat.in/httpdocs/uploads";
or
$path ="G:/PleskVhosts/abccat.in/httpdocs/uploads";
or
$path ="/httpdocs/uploads"; or $path ="/uploads";
mkdir($path, 0777, true);
I tried above all paths one by one, but didn't work. It is returning nothing as well. The full path for 'httpdocs' is G:/PleskVhosts/abccat.in/httpdocs.
Any help? Thanks.
PHP docs for mkdir() say that the mode is ignored on windows.
You may want to alter the permissions for the folders using chmod();
http://php.net/manual/en/function.mkdir.php
http://php.net/manual/en/function.chmod.php
Don't forget to set your permission back after you create your file.
chmod($folderPath, 0777); //<--This path would be for the folder you want your file in.
//You may have to do the chmod() for every folder all the way up to you target folder.
mkdir($filePath, 0777, true);
chmod($folderPath, 'mode'); //<---Put in the mode you need it to be. Do this for any folder you previously changed.
Try that and see if it helps.

Error creating directory using mkdir function in Codeigniter

I'm trying to recursively create a directory using php's mkdir function in a Codeigniter installation. My code looks like this:
mkdir('docs/client/bills/payd', 0777, true)
The docs directory already exists in my site root dir, the client directory is beeing created with 0755 permission, the bills directory is beeing created with permission 1341 (weird!) and the last directory, payd, is never created.
I tryed to change permission in the mkdir argument list to 0, 755, etc... and nothing has changed. I also tryed to set umask to 0, 0777... and nothing.
umask(0777);
mkdir('docs/client/bills/payd', 0777, true)
Can anyone please say what am I doing wrong? The code above is called from a Codeigniter regular controller.
Try with
if ( ! is_dir( FCPATH.'docs/client/bills/payd' )//FCPATH is absolute path to the project directory
{
mkdir( FCPATH.'docs/client/bills/payd', 0777, true );//although 0755 is just fine and recomended for uploading and reading
}
Use this to specify it the working directory, it might be confused as to where the directory is located.
mkdir( getcwd().'docs/client/bills/payd', 0777, true);
getcwd is the working directory for your codeigniter. You can search in the PHP guide the getcwd() function to make it clearer.
This should work.
EDIT
To make it clearer, that would return the following:
C:\xampp\htdocs\YOUR_ROOT_DIRECTORY\docs\client\bills\payd
EDIT AGAIN
Although after reading again, this would only create payd and assume that docs\client\bills is already created. You could create client and bills using mkdir or using the file explorer. But there are other PHP ways and I can help if needed.
Goodluck meyt
I also had this weird "1341" permissions error with PHP mkdir, nothing to do with CodeIgniter, it's a pure PHP issue!
After much experimentation, the only way I could get it to work was to include a slash at the end of the path, and set the recursive flag to 'true'. (Even though the PHP docs don't show a final slash, and I was only creating a single directory.)
I.e.
$existing_path = '/these/directories/already/exist/';
mkdir( $existing_path . 'new-directory/', 0755, true);

Create php directory at root using php

I have this folder tree:
assets/
scripts/
sites/
create.php
index.html
sites/
The create file (create.php) has the following in it:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$data = json_decode(file_get_contents("php://input"));
$fileName = $data->fileName;
$url = './sites/'.$fileName.'/';
mkdir('../../sites/'.$fileName);
I am trying to make a folder in the sites folder at the root of the website.
When I run the code above, I get this error:
Warning: mkdir(): No such file or directory in /Users/mikes/websites/simple/assets/scripts/sites/create.php on line 12
How the heck do I make a folder inside the sites folder at the base of the site?
UPDATE
Through some research, I got this.
mkdir($_SERVER['DOCUMENT_ROOT'] . '/simple/sites/'.$fileName);
I do not like that I have to include the '/simple/' in the url. It's not going to be very friendly when I upload this to my server. How would I automatically detect that?
mkdir(__DIR__.'/../../../sites/'.$fileName);
__DIR__ is a magic constant, that return the absolute path of the directory of the included file. You can read more about here.
Your're only going back 2 levels with ../../. you need to go back 3. ../../../
You may also want to make use of realpath('../../../sites/'.$fileName); to get an absolute URL if that helps.
Finally, you may want to check permissions of the directories that you want to mkdir in. most likely something like www-data group (for apache) and 0775 as the permission on the directory.

Creating folder outside /var/www

I'm trying to create folder outside of my www folder where are stored my php scripts.
<?php
$subfolder = $_POST['subfolder'];
mkdir("../Norme/".$subfolder, 0755, true);
?>
If I chech inside Norme folder there are no new folders created.
Folder Norme is placed in /var/Norme.
If I change mkdir statement to:
mkdir("Norme/".$subfolder, 0755, true);
The script creates folder Norme and given subfolder inside /var/www
How to set different folder than www folder?
Josef, Just change your code to:
`mkdir("/var/Norme/".$subfolder, 0755, true);
It should work. And make sure that the folder Norme has proper rights/permissions to create the folder inside it.

How to create folder in parent directory?

My php script is located in /var/www/html/users/dev. I need to create a folder in /var/www/images/ - something like /var/www/images/test/test/ and store here some images.
But when i trying is with mkdir($file_dir, 0777); where $file_dir is /var/www/images/test/test/ i receive an error:
Warning: mkdir(): No such file or directory in /var/www/html/users/dev/classes/sites.class.php...
Because "/var/www/images/test" does not exists, so you can not mkdir("/var/www/images/test/test")
You can specify the "$recursive" to TRUE, and it will work, like this:
mkdir($file_dir, 0777, TRUE);
Try
mkdir($file_dir, 0777, true);
The third parameter ('recursive') allows you to specify a path of which all directories will be created. If you don't, only the last directory ('test') will be created, and the whole path before that must exist.
The PHP documentation is quite clear about that.
if it is Linux you have set permissions to your parent directory 1st.
sudo chmod -R 777 /path of ur directory.

Categories