0

i am trying to create folders from within php. Whenever i use the mkdir function with permission 0777 i get a read-only folder. I want this folder to be read & write. The parent folder (drive/) is fully writable and readable for every user.

This is what i use: mkdir(ABSPATH . 'drive/' . $folderName, 0777);

I have also tried to use it without any additional paramaters: mkdir(ABSPATH . 'drive/' . $folderName);

Any ideas why this is and how to fix this so that i can generate folders that has write access?

f.lorenzo
  • 1,146
  • 2
  • 11
  • 24

3 Answers3

2

In a shared environment, mkdir fails to set the permissions properly. A workaround is to set the chmod with chmod() after you've created the directory.

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39
  • It works, thanks. Weird that in a share environment it fails to set it within the function but after file creation and chmodding it, it magically has read & write permission.. – f.lorenzo Aug 03 '14 at 13:22
1

You can have write permission too with mkdir..Here is the code

<?php
 $dir = 'myDir';
 // create new directory with 744 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under

 if ( !file_exists($dir) ) {
mkdir ($dir, 0744);
}

 file_put_contents ($dir.'/test.txt', 'Hello File');

Found the source from here

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
  • This is in accordance with the documentation: http://php.net/manual/en/function.mkdir.php. Note that the mode is ignored on Windows. – Gjermund Dahl Aug 03 '14 at 13:42
  • On Windows there are no real permissions like on UNIX. That's one of the reason I don't have any problems with my script doing file uploads and then re-arranging things and talk to ffmpeg, while on UNIX I do have problems with that. – Charlotte Dunois Aug 03 '14 at 13:53
1

the issue probably is the mask :)

if (!function_exists('mkdir_r')) {
    /**
     * create directory recursively
     * @param $dirName
     * @param int $rights
     * @param string $dir_separator
     * @return bool
     */
    function mkdir_r($dirName, $rights = 0744, $dir_separator = DIRECTORY_SEPARATOR) {
        $dirs = explode($dir_separator, $dirName);
        $dir = '';
        $created = false;
        foreach ($dirs as $part) {
            $dir .= $part . $dir_separator;
            if (!is_dir($dir) && strlen($dir) > 0) {
                $created = mkdir($dir, $rights);
            }
        }
        return $created;
    }
}

if (!function_exists('ensure_dir')) {
    /**
     * ensure directory exist if not create 
     * @param $dir_path
     * @param int $mode
     * @param bool $use_mask
     * @param int $mask
     * @return bool
     */
    function ensure_dir($dir_path, $mode = 0744, $use_mask = true, $mask = 0002) {
        // set new mask 
        $old_mask = $use_mask && $mask != null
            ? umask($mask)
            : null;
        try {
            return is_dir($dir_path) || mkdir_r($dir_path, $mode);
        } finally {
            if ($use_mask && $old_mask != null) {
                // restore original mask 
                umask($old_mask);
            }
        }
    }
}
ceph3us
  • 7,326
  • 3
  • 36
  • 43