1

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.

Joe
  • 4,852
  • 10
  • 63
  • 82
  • How is SAFE_MODE set? It normally gives errors because it can only upload to folders it created itself for safety reasons. – Marco Johannesen Sep 09 '11 at 09:21
  • Hi SAFE_MODE is `off` for the local value and 'on' for the master value. When you say "it can only upload to folders it created itself", what is it 'it' you are refering to? Also, I have tried to move my test code to the same folder as the working code and I still get the same error. – Joe Sep 09 '11 at 09:30
  • Hi ! Appart for a directory permission, I do not see reason for the tests failure. what OS are you running your scripts on ? Have you tried to run your working from the tests directory ? – Benjamin Dubois Sep 09 '11 at 09:51
  • Hi, thanks for your reply. Yes I have tried to run the working script from the tests directory and it works fine. I'm not sure of the OS. Rather annoyingly I have only been given ftp access to the server so I can't use command line to explore. – Joe Sep 09 '11 at 09:58

3 Answers3

1

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");
}
Community
  • 1
  • 1
Joe
  • 4,852
  • 10
  • 63
  • 82
0

In your test

fwrite("Blah");

should be

fwrite($fp, "Blah");

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

Nosrac
  • 144
  • 6
  • That's true thanks, but it's failing on the line before that and the original code is correct. I'll update the post. – Joe Sep 09 '11 at 11:10
0

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345