代码之家  ›  专栏  ›  技术社区  ›  datasn.io

为什么会出现权限被拒绝但仍成功打开文件的警告?

  •  3
  • datasn.io  · 技术社区  · 17 年前

    但是,尽管显示了警告消息,我的PHP脚本还是成功地打开了该文件,并在其中写入了一个字符串。这没有道理。

    那又有什么关系呢?我可以在fopen()前面加一个@,但这仍然很奇怪,我想知道PHP为什么会这样。是否有我没有正确配置的内容?

    class XMLDB {
    
        private $file = null;
        private $xml = null;
        private $defs = array();
        private $recs = array();
    
        // private members above, public members below
    
        public function __construct($xmlfile) {
            if (!file_exists($xmlfile)) {
                die('XML file does not exist.');
            }
            $this -> file = $xmlfile;
            $this -> xml = simplexml_load_file($this -> file);
            $this -> iniVocab();
            $this -> iniData();
        }
    

    ... /* 很多私人和公共的功能*/

        public function commit() {
            $xmlfile = fopen($this -> file, 'w'); // this is causing the warning
            $doc = new DOMDocument('1.0');
            $doc -> preserveWhiteSpace = false;
            $doc -> loadXML($this -> xml -> asXML());
            $doc -> formatOutput = true;
            fwrite($xmlfile, $doc->saveXML());
        }
    
        public function __destruct() {
            $this -> commit();
            /* comment this line out and there won't be any warnings, 
            /* therefore it should trace back to here. So I found out that
            /* it's when I use die() that eventually calls __destruct()
            /* which in turn calls commit() to trigger this fopen warning. */
        }
    }
    

    编辑:所以每次我尝试向打开的文件写入内容时,都没关系。然后,如果该类在卸载页面时再次尝试提交对文件的所有更改,即要销毁的对象,它将调用u destruct()方法和$this->commit()将更改写入文件-发生错误时,它拒绝写入文件并弹出“权限被拒绝”消息。真奇怪。

    4 回复  |  直到 17 年前
        1
  •  0
  •   Vladislav Rastrusny    17 年前

    无论此文件是否存在,您确定每次都会收到此“权限被拒绝”吗?此文件所在的文件夹权限是什么?例如,您可能已授予对它的读取权限,但没有执行权限。

        2
  •  0
  •   Quamis    17 年前

    尝试使用文件内容()。。。。但我相信它会发出同样的警告。。。

        3
  •  0
  •   Gus    17 年前

    当你在_destruct之外这样做时,它会给你警告吗?可能与此有关。或者apache有写权限但没有读权限?

        4
  •  0
  •   Martin Nycander    14 年前

    问题可能是您忘记使用关闭文件 fclose .

    既然你打开了这个文件,写一些东西,然后试着打开你已经打开的文件。 这可能是权限被拒绝的原因。

    public function commit() {
        $xmlfile = fopen($this -> file, 'w');
        $doc = new DOMDocument('1.0');
        $doc -> preserveWhiteSpace = false;
        $doc -> loadXML($this -> xml -> asXML());
        $doc -> formatOutput = true;
        fwrite($xmlfile, $doc->saveXML());
        fclose($this -> file);
    }
    
    推荐文章