Create directory with permission in php

mkdir

Attempts to create the directory specified by directory .

Parameters

The permissions are 0777 by default, which means the widest possible access. For more information on permissions, read the details on the chmod() page.

Note:

permissions is ignored on Windows.

Note that you probably want to specify the permissions as an octal number, which means it should have a leading zero. The permissions is also modified by the current umask, which you can change using umask() .

Allows the creation of nested directories specified in the directory .

Return Values

Returns true on success or false on failure.

Errors/Exceptions

Emits an E_WARNING level error if the directory already exists.

Emits an E_WARNING level error if the relevant permissions prevent creating the directory.

Examples

Example #1 mkdir() example

Example #2 mkdir() using the recursive parameter

// Desired directory structure
$structure = ‘./depth1/depth2/depth3/’ ;

// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.

if (! mkdir ( $structure , 0777 , true )) die( ‘Failed to create directories. ‘ );
>

See Also

User Contributed Notes 40 notes

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:

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:

mkdir ( ‘/test1/test2’ , 0777 , true );
chmod ( ‘/test1’ , 0777 );
chmod ( ‘/test1/test2’ , 0777 );
?>

Please note that in a shared environment I failed to take into account an existing umask when I did a mkdir(dirname, 0755). This ended up creating the directory (function returned true), but I didn’t have rights to do anything inside the folder, nor could I even view that it existed via ftp.

However, file_exists(dirname) returned true. Eventually I figured out what happened and was able to rmdir(dirname), then created the directory correctly.

So, when writing scripts you expect to be portable, either use umask to set your umask accordingly, or do a straight mkdir(dirname) followed by chmod(dirname, 0755) (or whatever it is you’re looking for). If you make the same mistake I did, you should be able to rmdir() or chmod() the folder so it’s accessible.

One small correction on a note from Frank in June 2006 on recursive directories under Windows.

First — this should be in the documentation as its the only function that I know of that php does not fix the slashes automatically.

$mypath = «testdir\subdir\test» ;
mkdir ( $mypath , 0777 , TRUE );
?>

This doesn’t work in windows:

$mypath = «testdir/subdir/test» ;
mkdir ( $mypath , 0777 , TRUE );
?>

This will work a bit better 🙂

$mypath = «testdir\\subdir\\test» ;
mkdir ( $mypath , 0777 , TRUE );
?>

When I created folder on windows with mkdir, I found some problem from folder nam so I write this function.

function filename_safe ( $name ) <
$except = array( ‘\\’ , ‘/’ , ‘:’ , ‘*’ , ‘?’ , ‘»‘ , » , ‘|’ );
return str_replace ( $except , » , $name );
>
?>

It can use with another functions about file system as file_put_contents to clean up file name.

One use case:
function addDir ( $path ) return (! mkdir ( $path , 0755 ) && ! is_dir ( $path ));
>
?>

Источник

mkdir

Attempts to create the directory specified by directory .

Parameters

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

The permissions are 0777 by default, which means the widest possible access. For more information on permissions, read the details on the chmod() page.

Note:

permissions is ignored on Windows.

Note that you probably want to specify the permissions as an octal number, which means it should have a leading zero. The permissions is also modified by the current umask, which you can change using umask() .

If true , then any parent directories to the directory specified will also be created, with the same permissions.

Return Values

Returns true on success or false on failure.

Note:

If the directory to be created already exists, that is considered an error and false will still be returned. Use is_dir() or file_exists() to check if the directory already exists before trying to create it.

Errors/Exceptions

Emits an E_WARNING level error if the directory already exists.

Emits an E_WARNING level error if the relevant permissions prevent creating the directory.

Examples

Example #1 mkdir() example

Example #2 mkdir() using the recursive parameter

// Desired directory structure
$structure = ‘./depth1/depth2/depth3/’ ;

// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.

if (! mkdir ( $structure , 0777 , true )) die( ‘Failed to create directories. ‘ );
>

See Also

  • is_dir() — Tells whether the filename is a directory
  • rmdir() — Removes directory
  • umask() — Changes the current umask

User Contributed Notes 5 notes

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:

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:

mkdir ( ‘/test1/test2’ , 0777 , true );
chmod ( ‘/test1’ , 0777 );
chmod ( ‘/test1/test2’ , 0777 );
?>

This is an annotation from Stig Bakken:

The mode on your directory is affected by your current umask. It will end
up having ( and (not )). If you want to create one
that is publicly readable, do something like this:

$oldumask = umask ( 0 );
mkdir ( ‘mydir’ , 0777 ); // or even 01777 so you get the sticky bit set
umask ( $oldumask );
?>

mkdir, file rw, permission related notes for Fedora 3////
If you are using Fedora 3 and are facing permission problems, better check if SElinux is enabled on ur system. It add an additional layer of security and as a result PHP cant write to the folder eventhough it has 777 permissions. It took me almost a week to deal with this!

If you are not sure google for SElinux or ‘disabling SELinux’ and it may be the cure! Best of luck!

Remember to use clearstatcache()

. when working with filesystem functions.

Otherwise, as an example, you can get an error creating a folder (using mkdir) just after deleting it (using rmdir).

When creating a file using mkdir() the default root will be the DocumentRoot (in XAMPP) itself.

If you use mkdir(«myfile») in something.php, instead of creating the folder in includes, php will create it in the project folder

Источник

Php create dirktery and give permmistion in linux

Then check if that user is either the owner or in the group of the folder where you want to write files. After doing this, you may find yourself unable to access the web folder if you access the server with a user other than www-data and other than root (like «webeditor»), and that user is neither the owner nor in the group.

Php permissions to create a file

Writing into a folder requires the Apache user to have writing, reading and executing privileges on that folder.

  1. So, first try to identify the name of the Apache user (often www-data).
  2. Then check if that user is either the owner or in the group of the folder where you want to write files.
  3. Give write, read and execute (7) privileges on that folder for that user. Give everyone else who don’t need writing the read and execute privileges (5) on the same folder.
  4. (recommended) Give write and read (6) privileges to your files for the www-data user. Everyone else only need read privileges (4).

If www-data is neither the owner nor in the group of the file, then you should change either one of them. After doing this, you may find yourself unable to access the web folder if you access the server with a user other than www-data and other than root (like «webeditor»), and that user is neither the owner nor in the group.

    Set the owner and group to the Apache user/group.

chown -R www-data:www-data /var/www 
usermod -a -G www-data webeditor 
find /var/www -type d -exec chmod 755 <> \; 
find /var/www -type f -exec chmod 644 <> \; 

Php — How do I set write permissions for a directory in, First of all check the directory permission by running the following command on terminal: stat -c ‘%A %a %n’ *. It will give the directory permission like: -rw-r—r— 644 \ drwxrwxr-x 775 Code Backup drwxr-xr-x 755 Desktop drwxr-xr-x 755 Documents. And again try to change the permission by sudo like:

How to give apache permission to write to home directory?

As your file residing in your Home directory, I would suggest one of following approaches.

chmod 0777 /home/djameson/test.txt 
sudo chown www-data:www-data /home/djameson/test.txt chmod 0744 /home/djameson/test.txt 
sudo usermod -a -G www-data djameson chmod 0764 /home/djameson/test.txt 

NOTE : I am assuming apache user name & group name is www-data & www-data respectively. You must change accordingly your server apache username/group name.

By default, Apache on Ubuntu runs as www-data .

Let’s assume your folder is located in /var/www/mysite .

You can do this:

chown -R www-data:www-data /var/www/mysite

chmod -R og-r

/var/www/mysite After doing this, www-data (the Web server) will have full access to the site’s files, while other non-root users will have no access at all.

If you wish to allow select users to access the site, you can make the folder group-readable and add those users to the group www-data .

Set correct permissions on your apache files

Never ever use 777 on web accessible files/directories that opens the doors for hacking.

How to give full directory and sub permissions in linux, # Change permissions for the root file/dir only chmod 777 path/to/directory/ # Or chmod 777 path/to/file # Change permission for root dir and all files/dirs within chmod -R 777 path/to/directory/

PHP — Permission denied on directory created via SSH

The right labeling for files and directories accessible from the httpd apache processes is httpd_sys_content_t ; while the files generated have user_tmp_t :

ls -Z drwxr-xr-x. amateous psacln system_u:object_r:httpd_sys_rw_content_t:s0 test drwxr-xr-x. amateous psacln unconfined_u:object_r:user_tmp_t:s0 testtest 

To fix the labeling, run (more info):

chcon -t httpd_sys_content_t

File — Create folder using PHP, Add a comment. 16. You can create a directory with PHP using the mkdir () function. mkdir («/path/to/my/dir», 0700); You can use fopen () to create a file inside that directory with the use of the mode w. fopen (‘myfile.txt’, ‘w’); w : Open for writing only; place the file pointer at the beginning of the file and truncate the file …

Allowing PHP to change file and directory ownership and permission

Well, it certainly sounds like a dangerous idea to begin with and I’d prefer sitting down and thinking through the whole strategy of what is trying to be achieved.

The danger is privilege escalation of an executable script which a remote user could modify or upload, of course. Full chown/chmod in a web app is equivalent to just pasting your root password on the page.

What is it exactly which needs to happen?

If the chown needs to happen for some reason but not to root (we hope) then the functionality should be wrapped. I would take the user requests and queue them, then have a separate process (could be shell, php, perl, anything) running as root by cron check this queue, check to see if the request fit the allowed parameters, and make the changes.

One way would be to set up sudo on your machine (assuming it’s a Linux box). Sudo allows you to run commands elevated, governed by restrictions set forth in the sudoers.conf file. Use tight rules to limit its use to the required commands in a specific directory for the user your web service is running under (like www-data), and then call the command shell from your PHP script something like tis:

shell_exec("sudo chmod 777 dirname"); 

Do make sure that your sudo config is tight, to ensure that breaking out will be next to impossible.

Perhaps you should look at the php commands: chmod, chown, chgrp and fileperms

chmod("/somedir/somefile", 0600); 

Php — Create a folder if it doesn’t already exist, Then it will attempt to create the next directory in that directory, and continue till it’s created all the directories. It returns true if successful. It returns true if successful. It could be improved by providing a stopping level so it just fails if it goes beyond the user folder or something and by including permissions.

Источник

Читайте также:  Передача параметров от скрипта скрипту php
Оцените статью