dpwfh874876 2013-12-29 22:43
浏览 22
已采纳

文本文件内容将回显但不返回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.

  • 写回答

1条回答 默认 最新

  • duanlin6989 2013-12-29 23:27
    关注

    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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?