PHP: mkdir() permission denied - php

I know there are a lot of topics about this 'problem', and I tried every solution proposed here: PHP mkdir: Permission denied problem But I still get the permission denied when I'm trying to make a folder using a PHP script.
http://i.prntscr.com/b5f37f0ff84f471bb62f250369c41625.png
For testing i've put everything under 777. albums is the one where I have to make sub dirs. In this case it's 755, but it also doesn't work with 777.
Really don't know what I can do next since I've been looking on google en SO for a few hours but still haven't got anything working.
My code to create the dir is as follows:
$target_path = DEFAULT_UPLOAD_PATH . $albumId . '/';
// albums/{xxxx-xxxx-xxxx}/
if (!is_dir($target_path)) {
mkdir($target_path, 0755, true);
print_r(error_get_last());
}
Thanks in advance!

The problem had nothing to do with permissions, but with the location of the target path. It was a relative path, but I converted it to an absolute path with the $_SERVER['DOCUMENT_ROOT'] variable.

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);

PHP Script is creating a folder but cannot create a folder inside

I have searched stack overflow and Google for many hours now and cannot find an answer. I have found things that are related but nothing is working.
Here is the code:
$oldmask = umask(0);
if(!is_dir("play")){
mkdir("play", 0777, true);
chmod("play", 0777);
}
if(!is_dir("play/playTest")){
mkdir("play/playTest", 0777, true);
chmod("play/playTest", 0777);
}
umask($oldmask);
The directory "play" is created fine, however I get this error when it tries to create the "play/playTest" directory.
SAFE MODE Restriction in effect. The script whose uid/gid is 178245/178245 is not allowed to access /a/b/c/play owned by uid/gid 25000/25000 in /a/b/c/script.php
I understand this is a file owner restriction due to safe mode, but why would the user be different when the folder was created in the same script?
I have tried with and without umask and with and without chmod, and many other things but nothing has worked.
Any and all help would be greatly appreciated, thanks.
This hacky workaround relies on a safe-mode vulnerability:
(you can recursively create directories if you do it using FTP)
http://php.net/manual/en/function.mkdir.php#104301

PHP rename directory failed

Well my PHP script generated an error with a hyperlink in it.
Does anyone know what's wrong?
PHP Warning: rename(./uploads/temp/00013/,./uploads/orders/39/) [<a href='function.rename'>function.rename</a>]: No such file or directory
update:
actual code in PHP
if(!file_exists('uploads/orders/')) {
mkdir('uploads/orders/'); // ensuring the orders folder exist
}
rename('uploads/temp/' . $u . '/', 'uploads/orders/' . $i . '/');
update:
Sorry, my fault. I coded to delete previous temp folder before this code execute. Thanks!
It seems that one (or both) of these directories don't exist:
uploads/temp/00013
uploads/orders/39
Have you checked that:
these directories exist?
Apache/PHP has permission to read/write in these directories?
Your current directory is really the parent directory of your "upload" directory?
When a computer tells you
No such file or directory
the first thing you should check is if the file/directory exists. This is not a random error message, it's given only in the specific situation when a file or directory you try to use does not exist.
In this case in particular, both ./uploads/temp/00013/ and ./uploads/orders/ have to exist. If orders doesn't exist it's not created for you.

Move a file and rename it

Figured PHP's rename would be my best bet. I didn't see many examples on how to use relative URLs in it though, so I kind of compromised. Either way, this give me permission denied:
I want to do this:
$file = "../data.csv";
rename("$file", "../history/newname.csv");
Where ../ of course would go back 1 directory from where the script is being ran. I couldn't figure out a way...so I did this instead:
$file = "data.csv";
$path = dirname(realpath("../".$file));
rename("$path/$file", "$path/history/newname.csv");
However I am getting permission denied (yes the history folder is owned by www-data, and yes data.csv is owned by www-data). I thought it was weird so I tried a simple test:
rename( 'tempfile.txt', 'tempfile2.txt' );
and I made sure www-data had full control over tempfile.txt...still got permission denied. Why? does the file your renaming it to have to exist? can you not rename like linux's mv? So I instead just copy() and unlink()?
In order to move a file from "../" to "../history/", a process needs write permission to both "../" and "../history/".
In your example, you're obviously lacking write permission to "../". Permissions for the file being moved are not relevant, by the way.
Not only ownership plays a role, but also file permissions. Make sure that the permissions are set up correctly on the source file and destination directory (e.g. chmod 644 data.csv).
Is www-data the same user as Apache?
Edit: Take care to provide existing, absolute paths to realpath(). Also beware of the following:
$path = dirname(realpath("../".$file));
This might yield nothing, because the file ../data.csv might not exist. I.e., the result of realpath() on a non-existent file is false.
Here's some code that might work better for you:
$file = "data.csv";
$path1 = realpath($file);
$path2 = realpath(dirname($file).'/..').'/history/newname.csv';
rename($path1, $path2);
You should be extremely careful that $file cannot be edited by the visitor, because he could change a request manipulate which file is renamed to where.

Categories