PHP - 写入文件 - 一个脚本的权限被拒绝,但同一目录中的另一个脚本的权限不被拒绝

发布于 2024-12-03 13:56:30 字数 1127 浏览 1 评论 0原文

我的 PHP 脚本之一将数据写入文件。它正在执行,没有问题。但是,我的测试代码以相同的方式调用相同的方法并且(为了尝试解决此问题)位于同一目录中,正在报告以下

failed to open stream: Permission denied

错误:

我正确执行的脚本很简单:

try {
    $printer = new PDFPrinter();
    $FDFData = $printer->assembleFDFData(5);
    $fdf_file = $printer->writeFDFFile($FDFData);
    $printer->downloadForm($fdf_file);
}catch(Exception $e) {
    echo $e->getMessage();
}

我的测试代码是:

    function setUp() {
        $this->printer = new PDFPrinter();
        $this->FDFData = $this->printer->assembleFDFData(5);
    }

    function testWrite() {
        try {
            $this->printer->writeFDFFile($this->FDFData);
            $this-assertTrue(true);
        } catch(Exception $e) {
            $this->assertTrue(false);
        }
    }

我知道 testWrite 测试代码很糟糕,但我只想让它首先停止抛出该异常。

当脚本尝试打开要写入的文件时执行 writeFD​​FFile($this->FDFData); 方法时会引发异常。这是失败的代码:

$fp=fopen($fdf_file,'w')

我不明白同一目录中具有相同权限的两个文件在执行相同的代码时如何以不同的方式工作。

我可能做错了什么?

非常感谢

One of my PHP scripts writes data to a file. It is executing without issues. However my test code which calls the same methods in the same way and (for the purpose of trying to clear up this problem) is located in the same directory is reporting a:

failed to open stream: Permission denied

error.

My correctly executing script is simply:

try {
    $printer = new PDFPrinter();
    $FDFData = $printer->assembleFDFData(5);
    $fdf_file = $printer->writeFDFFile($FDFData);
    $printer->downloadForm($fdf_file);
}catch(Exception $e) {
    echo $e->getMessage();
}

and my test code is:

    function setUp() {
        $this->printer = new PDFPrinter();
        $this->FDFData = $this->printer->assembleFDFData(5);
    }

    function testWrite() {
        try {
            $this->printer->writeFDFFile($this->FDFData);
            $this-assertTrue(true);
        } catch(Exception $e) {
            $this->assertTrue(false);
        }
    }

I know the testWrite test code is bad, but I just want to get it to stop throwing that exception first.

The script throws the exception when executing the writeFDFFile($this->FDFData); method as it tries to open the file to write. This is the code which fails:

$fp=fopen($fdf_file,'w')

I don't understand how 2 files in the same directory with the same permissions can work differently when executing the same code.

What could I be doing wrong?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2024-12-10 13:56:30

如何运行标准代码和测试代码?也许您通过 apache 运行标准代码(在浏览器中打开),但直接通过 php 测试代码(从 IDE 运行)。也许apache有权限,但php没有。也许工作目录有所不同($fdf_file 包含绝对路径?)。

另请注意,“无法打开流:权限被拒绝”是警告(不是致命错误),当您运行标准代码时,此警告不会停止执行。但是 PHPUnit 将警告转换为异常,当您在 PHPUnit 下运行代码时,此警告将停止执行。

How do you run standard and test code? Maybe you run standard code through apache (open in browser), but test code through directly php (run from IDE). And maybe apache have permissions, but php is not. And maybe working directory is difference ($fdf_file contains absolute path?).

Note also that "failed to open stream: Permission denied" is warning (not fatal error) and when you run standart code this warning will not stop execution. But PHPUnit convert warnings to exceptions and this warning will stop execution when you run code under PHPUnit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文