下面是一个简单的数据库类,也是它的一部分,除了与我的问题相关的部分外,我将所有内容全部删除。无法让PHP写入文件
基本上,它允许我创建一个文件,其中包含在页面上运行的所有查询,包括它们的时间,顺序,总数以及实际运行的SQL。这是非常好的,但我有麻烦。我无法将它写入文件,您可以在下面看到我打印的是$ sss的值,这是应写入文件的值。在PHP中,我有错误报告设置显示一切,我没有写入错误或任何东西,它只是不写入文件,可能我可能有文件在错误的文件夹,但我不知道。
那么将文件路径设置为日志文件并将其写入的好方法是什么?如果它现在找不到该文件,我是否应该看到某种错误?
所有的<?PHP
class Database
{
public $debug = true;
public $query_count = 0;
public $debug_file = "debug.sql";
public function query($sql){
//start timer stuff for debug logger
list($usec, $sec) = explode(" ",microtime());
$time_start = ((float)$usec + (float)$sec);
//run mysql query
$result = mysql_query($sql);
///debug
list($usec, $sec) = explode(" ",microtime());
$time_end = ((float)$usec + (float)$sec);
$time = $time_end - $time_start;
if($this->debug){
$this->query_count ++;
$f = fopen($this->debug_file, "a");
$sss = "# ".$this->query_count."\n ".$time." sec \n\n".$sql."\n#-------------------------------------------------------------------------\n\n";
// I print to screen just for test to see what should be wrriten to the file
echo $sss;
fputs($f, $sss, strlen($sss));
fclose($f);
}
//end debug
}
}
?>
代码看起来不错。如果文件无法找到或打开,您应该会收到警告。你的error_reporting设置为什么?你有权写入当前目录吗? – 2010-01-17 22:04:25