简体   繁体   English

文本文件内容将回显,但不会返回php

[英]Text file contents will echo but not return php

I've been working o this for the last few weeks and can't find an alternate route. 在过去的几周里,我一直在为此工作,找不到替代路线。 What I need to do is return the contents of a text file after the file has been read. 我需要做的是在读取文本文件后返回文本文件的内容。 I have two different logs that use text files to log errors. 我有两个使用文本文件记录错误的不同日志。 The first log returns the correct variable that I ask for but for some reason, even though I use the exact same methods to call the variable, it doesn't return anything. 第一个日志返回我要求的正确变量,但是由于某种原因,即使我使用完全相同的方法来调用该变量,也不会返回任何内容。 If I echo the variable then the correct string is displayed but the variable returns nothing. 如果我回显该变量,则显示正确的字符串,但该变量不返回任何内容。 Here is the function: 这是函数:

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";
    }               
}

Why does this function return the necessary string when executed in one function but has to be echoed to see content in the other is my question. 为什么在一个函数中执行此函数时会返回必要的字符串,但是必须回显才能看到另一个函数中的内容,这是我的问题。

Try changing 尝试改变

$fclose($file);

to this 对此

fclose($file);

I was able to get the code to work on my server. 我能够使代码在服务器上工作。 The only change I made here is the path to the file which is in the same directory as the PHP script. 我在这里所做的唯一更改是文件的路径,该路径与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;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM