过去几周我一直在工作,无法找到替代路线。我需要做的是在读取文件后返回文本文件的内容。我有两个不同的日志,使用文本文件来记录错误。第一个日志返回我要求的正确变量,但由于某种原因,即使我使用完全相同的方法来调用变量,它也不会返回任何内容。如果我回显变量,则显示正确的字符串,但变量不返回任何内容。这是功能:
function GetNoticeLog($strDate){
$logdate = preg_replace("/[^0-9]/", "_", $strDate );
$strFileName = realpath('debuglogs/enotice_logs').'/ENOTICELOG_' . $logdate . '.txt';
if(is_readable($strFileName)){
$file = fopen($strFileName,"r");
$contents = fread($file, filesize($strFileName));
$fclose($file);
return nl2br($contents);
}
else if(!is_readable($strFileName)){
echo $strFileName." is unreadable";
}
}
为什么这个函数在一个函数中执行时返回必要的字符串,但是必须回显才能看到另一个函数中的内容是我的问题。
答案 0 :(得分:0)
尝试更改
$fclose($file);
到这个
fclose($file);
我能够让代码在我的服务器上运行。我在这里做的唯一更改是文件的路径,该文件与PHP脚本位于同一目录中。
<?php
function GetNoticeLog($strDate){
$logdate = preg_replace("/[^0-9]/", "_", $strDate );
//$strFileName = realpath('debuglogs/enotice_logs').'/ENOTICELOG_' . $logdate . '.txt';
$strFileName = "blah.txt";
if(is_readable($strFileName)){
$file = fopen($strFileName,"r");
$contents = fread($file, filesize($strFileName));
fclose($file);
return nl2br($contents);
}
else if(!is_readable($strFileName)){
echo $strFileName." is unreadable";
}
}
$stuff = GetNoticeLog("whatever");
echo $stuff;
?>