Joe
Joe

Reputation: 4928

PHP fopen succeeding for one script but not another

My following method uses an array of data $FDFData to create an FDF file:

    public function writeFDFFile($FDFData) {
        $fdf_file = time().'.fdf';
        $fdf = $this->createFDF(PDF_FORM, $FDFData);

        // write the file out
        if($fp=fopen($fdf_file,'w')) {
            fwrite($fp,$fdf,strlen($fdf));
        } else {
            throw new Exception('Unable to create file: '.$fdf_file);
        }

        fclose($fp);
        return $fdf_file;
    }

One of my scripts runs absolutely fine:

require_once('PDFPrinter.php');
try {
    $printer = new PDFPrinter();
    $FDFData = $printer->assembleFDFData(9);
    $fdf_file = $printer->writeFDFFile($FDFData);
    $printer->downloadFile($fdf_file);
}catch(Exception $e) {
    echo $e->getMessage();
}

but I can't get my test code to run because I get a:

Unexpected PHP error [fopen(1315558352.fdf) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied]

error thrown:

require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once(dirname(__FILE__) . '/../PDFPrinter.php');
class PDFPrinterTest extends UnitTestCase {

    private $printer;
    private $FDFData;

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

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

Both scripts are run in directories with the correct permissions. I am running my test scripts through the browser as well, so it's not that I have a different environment.

I'm stuck as to how to proceed to find the issue really.

My directory structure:

HOME - PDFPrinter.php
  |
  -----tests - PDFPrinterTest.php
         |
         ------simpletest - autorun.php

Any suggestions as to how I could find the issue?

Many thanks

Update

I have tried changing my test class so that the only test function in there is:

function testWrite() {
    try {
        $name = "testing.txt";
        if($fp=fopen($name, 'w')) {
            fwrite($fp, "Blah");
        } else {
            throw new Exception("Nope.");
        }
    $this->pass("All good");
    } catch(Exception $e) {
        $this->fail("Not good");
    }

}

but the exception is still thrown with the warning.

Yet a very simple script run from the same directory works fine:

$name = "Test.txt";
if($fp=fopen($name, 'w')) {
    fwrite($fp, "Working");
} else {
    throw new Exception("Nope.");
}
fclose($fp);

that will actually create and write to the file.

Upvotes: 1

Views: 646

Answers (3)

Joe
Joe

Reputation: 4928

Finally found the solution which was that the file name needed to be the full absolute address in order for it to work in both scripts for some reason. This was suggested in one of the answers for this SO question which I quote below:

Use fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','a+');

so for my code, I have used:

if($fp=fopen($_SERVER['DOCUMENT_ROOT'].$name, 'w')) {
    fwrite($fp, "Working");
}

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 158005

failed to open stream: Permission denied

There is one important programmer's skill every developer ought to master.
Here it is:

To trust your eyes

If your PHP telling you that permission is denied - so it is. Just doble-check it. It is not a big deal yet noone can do it for you.

Upvotes: 0

Nosrac
Nosrac

Reputation: 144

In your test

fwrite("Blah");

should be

fwrite($fp, "Blah");

I'm not sure what the problem in the original code is though.

Upvotes: 0

Related Questions