【发布时间】:2011-11-13 15:24:23
【问题描述】:
我下面的方法使用了一个数据数组$FDFData来创建一个FDF文件:
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;
}
我的一个脚本运行得非常好:
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();
}
但我无法运行我的测试代码,因为我得到了:
Unexpected PHP error [fopen(1315558352.fdf) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied]
抛出错误:
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);
}
}
}
这两个脚本都在具有正确权限的目录中运行。我也在通过浏览器运行我的测试脚本,所以并不是我有不同的环境。
我不知道如何真正找到问题。
我的目录结构:
HOME - PDFPrinter.php
|
-----tests - PDFPrinterTest.php
|
------simpletest - autorun.php
关于如何找到问题的任何建议?
非常感谢
更新
我已尝试更改我的测试类,以便其中唯一的测试功能是:
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");
}
}
但仍会抛出异常并发出警告。
然而,从同一目录运行的一个非常简单的脚本可以正常工作:
$name = "Test.txt";
if($fp=fopen($name, 'w')) {
fwrite($fp, "Working");
} else {
throw new Exception("Nope.");
}
fclose($fp);
这将实际创建并写入文件。
【问题讨论】:
-
SAFE_MODE 是如何设置的?它通常会出错,因为出于安全原因,它只能上传到它自己创建的文件夹。
-
嗨,SAFE_MODE 是
off,表示本地值,“on”表示主值。当您说“它只能上传到它自己创建的文件夹”时,您指的是什么“它”?另外,我尝试将我的测试代码移动到与工作代码相同的文件夹中,但仍然出现相同的错误。 -
嗨! Appart 获得目录权限,我看不到测试失败的原因。你在什么操作系统上运行你的脚本?您是否尝试过从测试目录运行您的工作?
-
您好,感谢您的回复。是的,我尝试从测试目录运行工作脚本,它工作正常。我不确定操作系统。相当烦人的是,我只获得了对服务器的 ftp 访问权限,因此我无法使用命令行进行探索。
标签: php permissions fopen