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);
Related
How do I set permissions for folders below 777?
mkdir(getcwd() . '/public/profile/usr'.$user->getId(), 0777, true);
mkdir(getcwd() . '/public/profile/usr'.$user->getId() . '/patients', 0777, true);
mkdir(getcwd() . '/public/profile/usr'.$user->getId() . '/picture', 0777, true);
Folder permissions for these are the type below:
drwxr-xr-x
And I want it set to:
drwxrwxrwx
Can you tell me please how to solve this problem?
First off, there is never a good reason to set folder or file permissions to 777. People that feel the need to do this usually either are blindly following a (bad) online tutorial or have an underlying security/permissions problem that they are unable to fix. Usually any uploading/writing problems can be fixed by setting the proper owner/group on the folder (usually the www-data or apache user), 777 means that anybody can write or execute anything in those folders. You're basically leaving your back door open and unlocked in a bad neighbourhood. Also see How will a server become vulnerable with chmod 777? for further details.
That being said, I've seen problems like this with mkdir() before, it is usually because of a certain umask on the system being set, forcing newly created folders to have 755 permissions. Doing a specific chmod() call will do the trick though:
mkdir(getcwd() . '/public/profile/usr'.$user->getId(), 0777, true);
chmod(getcwd() . '/public/profile/usr'.$user->getId(), 777);
// And so on...
But, the better solution: fix your permissions/security issue. Just set the proper owner (the process that PHP is running under), this will avoid having to set 777 permissions, but will allow proper upload handling. This should already be the default, but you can force it with:
// This will get the user the PHP process is running under on Linux machines:
$user = `whoami`;
mkdir(getcwd() . '/public/profile/usr'.$user->getId(), 0755, true);
chown(getcwd() . '/public/profile/usr'.$user->getId(), $user);
// And so on
This will set 755 permissions on the folder and set ownership to the PHP process, meaning that PHP has full permissions on the folder, but any other users do not. This is more secure, yet no less convenient.
The problem might also be caused by SELinux, in which case chmod 777 is not going to help you much anyway. Usually the httpd_public_content_rw_t context is required on upload folders.
Check this post: http://php.net/manual/de/function.mkdir.php
Answer from there...
When using the recursive parameter bear in mind that if you're using chmod() after mkdir() to set the mode without it being modified by the value of uchar() you need to call chmod() on all created directories. ie:
<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1/test2', 0777);
?>
May result in "/test1/test2" having a mode of 0777 but "/test1" still having a mode of 0755 from the mkdir() call. You'd need to do:
<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1', 0777);
chmod('/test1/test2', 0777);
?>
Here is one of the many possible ways of doing it:
<?php
$baseDir = getcwd() . '/public/profile/usr'. $user->getId();
$arrFolders = array('/patients', '/picture');
// FIRST chmod THE BASE DIRECTORY TO HAVE THE PERMISSIONS YOU WANT:
chmod($baseDir, 0777);
// LOOP THROUGH THE ARRAY OF FOLDERS YOU WANT TO CREATE & CREATE THEM WITH APPROPRIATE PERMISSIONS:
foreach($arrFolders as $folder){
try{
mkdir($baseDir . $folder, 0777, true);
}catch(Exception $e){
// HANDLE ANY EXCEPTION SHOULD ONE OCCUR...
}
}
I have folder hierarchy as Bucharest/Waterfall/a.php. My code snippet for making directory on server is as follows:
if(!is_dir($this->folder)){
$old_umask = umask(0);
mkdir($this->folder, 0777);
umask($old_umask);
}
chmod($this->folder, 0777);
// Moves current file to upload destination
if(move_uploaded_file($current['tmp_name'],$uploadFile))
return true;
All files are uploaded to the server. Now the issue is that the parent folder ,i.e. Bucharest has permission 755 while inner folder has permission 777. $this->folder has value as Bucharest/Waterfall. It gives 755 permission to Bucharest while 777 permission to waterfall. According to my code the 777 permission should also be given to Bucharest.
I have also tried chmod but all in vain. I want to give full permission to parent folder.
is_dir($this->folder); tells you that the file isn't a directory, not that it doesn't exist. and of course you can't mkdir if the directory is already there.
Also, if the directory already exists, according to the documentation chmod will not do anything if the user under which php is running isn't the same as the user who owns that directory.
Is the folder you are checking a subfolder already? If so, create the parent folders firts.
i.e.
mkdir('/tmp/test1/test2/test3/test4');
will fail if '/tmp/test1/test2/test3' doesn't exist
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);
For a project I'm implementing a file-upload system. For every user account I would like the script to create a different sub-folder. Lets say their user_id's.
Each time a user is added, the system will create a new sub-folder for their own uploads. For example:
Uploads/
- user1
- user2
- user3
By executing mkdir('Uploads/'.$user_id, 0777); it will create a new subfolder. Everything is fine.
However my application is not able to write to this folder. How do I have php make directories with the required file permissions? I have tried using chmod with no success.
This might help chmod and mkdir
$dirMode = 0777;
mkdir($directory, $dirMode, true);
// chmod the directory since it doesn't seem to work on recursive paths
chmod($directory, $dirMode);
For mkdir, mode is ignored on Windows. and 0777 is by default. and the third param is recursive which allows the creation of nested directories specified in the pathname.
sometimes the directory created with another mode than specified ( 0755 instead 0777 etc).
to solve that use :
<?php
$old = umask(0);
mkdir($dir,0777);
umask($old);
?>
I have my web application hosted in /var/www folder. I am creating a folder from one of the PHP scripts of the web application. The default permission of the created folder is drwx------, i.e. 700. But I want that folder to have at least 755 permission.
Up to now I tried: mkdir($path, 0755) and chmod($path, 0755) PHP functions but without any success.
Does anybody know how to solve my problem please?
Millions of thanks beforehand.
Have you tried changing the umask ?
Have a look here: http://nl3.php.net/manual/en/function.umask.php
The easiest way it to:
$oldmask = umask(0);
chmod($path, 0755);
umask($oldmask)
Since you have default permission of 700, which means the parent directory (the directory in which you are trying to create the folder) do not have rw permission for group owner or other users. Most often the running demon(httpd) is not the owner of the parent folder and hence cannot modify the directory.
In simple terms, the php script do not have access to modify or add new directory. You need to change the permission of the parent folder to at least drwxrw-rw- (or 0755).
Use ssh, cpanel or ftp client to do this. If you do it using php script you will end with the same problem again, as parent of parent will have again 0700. ;)