如何创建具有权限的文件然后更改权限


How can I create file with permission then change permission

我认为文件所有者有问题。但我不知道我应该如何更改代码。

我的职能:

public static function createFile($fileName, $mode = 0777 ){
        if (! is_string( $fileName ) || empty( $fileName )) {
            throw new Exception( "File name must be a string and can not be empty", 923050 );
        }
        $touchResult = touch( $fileName );
        if (! $touchResult) {
            throw new Exception( "Error occurs while touch method was executed", 923052 );
        }
        if (! is_int( $mode ) || $mode > 511) {
            throw new Exception( 'invalid mode value', 923051 );
        } else {
            $chmodResult = chmod( $fileName, $mode );
            if (! $chmodResult) {
                throw new Exception("Error occurs while chmod method was executed", 923052);
            }
        }
    }

测试:

public function testCreateFile(){
        $fileToCreate = __DIR__ . "/../../../../../logs/new.txt";
        //Delete file if exist
        if (file_exists( $fileToCreate )) {
            FileHandler::delete( $fileToCreate );
        }
        //Create file. Default mode 0777
        FileHandler::createFile( $fileToCreate );
        $this->assertFileExists( $fileToCreate );
        $filePermisson = substr( sprintf( '%o', fileperms( $fileToCreate ) ), - 4 );
        $this->assertEquals("0777", $filePermisson);
        //Change permission of existing file
        FileHandler::createFile($fileToCreate, 0775);
        $filePermisson = substr( sprintf( '%o', fileperms( $fileToCreate ) ), - 4 );
        $this->assertEquals("0775", $filePermisson);
    }

错误:

有 1 个失败:

1) 文件处理程序测试::测试创建文件失败断言"0777" 预计匹配"0775"。

fileperms();之前运行clearstatcache();函数以清除缓存