php设置权限文件以使其可写

php设置权限文件以使其可写,php,linux,permissions,filesystems,Php,Linux,Permissions,Filesystems,在用php编程多年之后,这个问题对我来说仍然很奇怪 我想让一个文件以友好的方式写,这样在生产/开发中就不会有权限问题。。。。但它仍然给我带来麻烦 有人能给我解释一下我错了什么吗 // permissions -- the folder writable by www-data //drwxrwxrwx 2 www-data www-data 4096 mag 24 12:19 Details // then the file not writable by the owner -r--

在用php编程多年之后,这个问题对我来说仍然很奇怪

我想让一个文件以友好的方式写,这样在生产/开发中就不会有权限问题。。。。但它仍然给我带来麻烦

有人能给我解释一下我错了什么吗

// permissions    -- the folder writable by www-data
//drwxrwxrwx 2 www-data www-data  4096 mag 24 12:19 Details

// then the file not writable by the owner
-r----x--t 1 www-data www-data 0 giu  8 12:48 /home/giuseppe/homeProj//Ariprova/trunk/PhpCsv/phpcsv/ConfigClasses/Helpers/Virtuemart2/Details/324-PartsToAdd.json

// then the code

if (!file_exists($fileRequest)) {   // it's found
                throw new Exception ('File non trovato. Nome File completo: '.$fileRequest. ' con cartella '.  getcwd()); 
                }  

if (!is_writable($fileRequest)) {
                $results = @chmod($fileRequest, '0777');  //this gives true 

            }

            $fileReq = fopen($fileRequest, 'w');
            fwrite($fileReq, 'a string' );   // this writes nothing on file

            fclose($fileReq);

以下是chmod的手册页: 您可以看到函数“尝试更改指定文件的模式”

这是因为可能不允许运行脚本的用户(例如apache)覆盖该目录所有者设置的权限(至少这可能是原因之一)


更新:您可以尝试与所有者(或root用户)一起转到父目录,并将所有权更改为您需要的用户(chown apache:apache dirname或类似内容)

以下是chmod的手册页: 您可以看到函数“尝试更改指定文件的模式”

这是因为可能不允许运行脚本的用户(例如apache)覆盖该目录所有者设置的权限(至少这可能是原因之一)


更新:您可以尝试与所有者(或root用户)一起转到父目录,并将所有权更改为您需要的用户(chown apache:apache dirname或类似的内容)

chmod($fileRequest,'0777')
更改为
chmod($fileRequest,0777)
。字符串
'0777'
将被转换为一个数值,该数值为777十进制,这不是您所期望的;您真正想要的是0777八进制。

chmod($fileRequest,'0777')
更改为
chmod($fileRequest,0777)
。字符串
'0777'
将被转换为一个数值,该数值为777十进制,这不是您所期望的;您真正想要的是0777八进制。

您是否尝试过使用
chmod
而不是
@chmod
(它会抑制错误消息)?还可以尝试chmod($fileRequest,777)@Rishabh 777十进制不是0777八进制。您是否尝试过使用
chmod
而不是
@chmod
(这会抑制错误消息)?也可以尝试chmod($fileRequest,777)@Rishabh 777十进制不是0777八进制。