Question

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.

Était-ce utile?

La solution

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

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;

?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top