0

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.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • So.. $contents is always correct (because you echoed it and you saw it's ok) but the return is empty? Seems impossible to me.. show the code that calls the function. Also.. you can just use file_get_contents() to read the file in one call. – cen Dec 29 '13 at 23:23

1 Answers1

0

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;

?>
Adlin Ling
  • 382
  • 1
  • 7
  • Thanks. I just found the answer. The misspelled a vital index that I used to display my forms. Thanks for your help. I sincerely appreciate it – user3144692 Dec 29 '13 at 23:40