PHP mkdir or chmod 0777 doesn't work - php

I have a small script that create a directory for a user who registers an account on my site. This director is used to store images in that they upload.
When I use mkdir() with permissions of 0777, the directory is created under the users name, but the permissions stay as 0755.
I have tried a few different ways to do this, as follows;
$path = path/to/directory/and/filename
$old = umask(0);
mkdir($path,0777);
umask($old);
also;
$path = path/to/directory/and/filename/
mkdir($path, 0777, true);
also;
$path = path/to/directory/and/filename/
mkdir($path);
chmod($path,0777);
When I create a directory myself in the ftp client the owner and group is me, but when the php script created them, it has the owner and group of apache/apache'. I don't think if this is anything to do with it?
I have spent a good while trying to figure this out, and I have also spent a fair amount of time searching on this forum and others, so any help would be amazing!
If there is any other information required, I can provide. Thanks!

Related

PHP: mkdir() permission denied

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.

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

MKDIR and CHMOD working , upload doesn't succeed

I have a really simple PHP script that creates a directory according to a product id, these folders are made to upload product id specific images into it.
Once this folder is made with PHP script mkdir('folder',0777) I upload an image with PHP to that just made folder. This doesn't work as it should : the move_uploaded_file function returns a regulation in the server safe_mode function. Although this the servers safe_modeproperty is turned off, is still gives this error / warning.
When I check with my FTP user account, I see the made directory with permission 777, but the uploads won't succees to upload to that directory...
Strangeness of it is that when i manually delete the made directory and make a new one (via FTP) the uploads work perfect!
Does anyone have any clue on fixing this issue? I'm not that server experienced :)
Thanks!
use this for mkdir username is your foldername in uplod folder
if(!is_dir('uploads/'.$username . "/")) {
mkdir('uploads/'.$username . "/", 0755);
}`
You have to take in account, that when creating a directory the create mask is anded with the umask:
$old = umask( 0 );
mkdir( 'folder', 0777, true );
umask( $old );
The mode on your directory may be being affected by your current umask. It will end
up having (mkdir-mode & (~umask)) permissions.
Try:
$oldmask = umask(0);
mkdir('folder', 0777);
umask($oldmask);

mkdir writing permissions

I have a small problem. I have searched stack overflow for similar things, but they don't seem to help (as far as I can tell).
I am using mkdir in php, which is working lovely and creating a new directory each month. However I have just noticed after the month changed that the directories are being created without write permissions. I believe this is to do with using 0777 to allow max access rights, however with this there is no change, and I believe this is set as default with out.
Here is the code:
if (!is_dir($this->config->item('rootpath').'/assets/documents/'.$date))
{
$subDirectory = mkdir($this->config->item('rootpath').'/assets/documents/'.$date, 0777);
}
else
$subDirectory = $this->config->item('rootpath').'/assets/documents/'.$date;
It is only after trying to upload a file, that the permission is denied, and I can go and set write permissions for the folder generated, and resubmit the file upload.
Add right permissions next to mkdir like this:
$subDirectory = mkdir( $this->config->item( 'rootpath' ) . '/assets/documents/' . $date, 0777 );

PHP create directory with 777 permission in Windows

I have read this:-
Why can't PHP create a directory with 777 permissions?
and I can see a new folder being created by applying the following:-
// Desired folder structure
$structure = "../../../".$flash_dir."HELLO";
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
$oldmask = umask(0);
mkdir($structure, 0777);
umask($oldmask);
when viewing the file permission of HELLO with DreamWeaver, it is 777. However, I suspect it is a Linux 0777 rather than a Windows 777, therefore I still cannot upload things to HELLO.
Will there be any alternative method to create a directory with windows 777? Thanks!
PS. when I manual create a new directory and right click it to set 777, it works perfectly, so I really think it's related to Linux vs Windows~
0777 is exactly the same thing as 777
But I still can't say what the problem is. I would try to chmod it again after you've created it.
$oldmask = umask(0);
chmod($structure, 0777);
umask($oldmask);

Categories