PHP - writing file - Permission denied for one script, but not another in the same directory
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 w开发者_如何学Cant 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
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.
精彩评论