mkdir always sets folders to read-only?

余生长醉 提交于 2019-12-20 05:52:11

问题


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?


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/25104990/mkdir-always-sets-folders-to-read-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!