【问题标题】:php set permissions file to make it writablephp设置权限文件使其可写
【发布时间】:2012-06-12 10:29:14
【问题描述】:

经过多年的 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 而不是@chmod(抑制错误消息)?
  • 也可以试试 chmod($fileRequest, 777);
  • @Rishabh 777 十进制不是 0777 八进制。

标签: php linux permissions filesystems


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这里是 chmod 的手册页:http://php.net/chmod 可以看到函数“Attempts改变指定文件的模式”

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

    更新:您可以尝试使用所有者(或确定为 root)转到父目录并将所有权更改为您需要能够执行此操作的用户(chown apache:apache dirname 或类似的东西)

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 2017-05-15
      • 2017-09-27
      • 2015-12-31
      • 2014-07-14
      • 2013-09-09
      • 2015-05-13
      • 2013-09-12
      • 1970-01-01
      相关资源
      最近更新 更多