2

Panique很好地回答了这个线程上的一个问题:来自远程服务器的 PHP 目录列表

但是他创建了一个我根本不理解的功能,并希望得到某种解释。例如,随机的 8192 数字是什么?

function get_text($filename) {

    $fp_load = fopen("$filename", "rb");

    if ( $fp_load ) {

            while ( !feof($fp_load) ) {
                $content .= fgets($fp_load, 8192);
            }

            fclose($fp_load);

            return $content;

    }
}
4

3 回答 3

1

It loads the file which path is in $filename and returns it's content. 8192 is not random. it means read the file in chunks of 8kb.

The while loop runs as long as the file wasn't entirely read. Each iteration adds the latest 8kb of the file to $content which is returned at the end of the function.

于 2013-07-06T19:25:57.610 回答
0

It loads a file's data.

For example, what's with the random 8192 number?

http://php.net/manual/en/function.fgets.php

Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

于 2013-07-06T19:26:32.433 回答
0

分解它:

function get_text($filename) { //Defines the function, taking one argument - Filename, a string.

    $fp_load = fopen("$filename", "rb"); //Opens the file, with "read binary" mode.

    if ( $fp_load ) { // If it's loaded - fopen() returns false on failure

            while ( !feof($fp_load) ) { //Loop through, while feof() == false- feof() is returning true when finding a End-Of-File pointer
                $content .= fgets($fp_load, 8192); // appends the following 8192 bits (or newline, or EOF.)
            } //Ends the loop - obviously.

            fclose($fp_load); //Closes the stream, to the file.

            return $content; //returns the content of the file, as appended and created in the loop. 

    } // ends if
} //ends function

我希望这有帮助。

详细说明8192:

当读取到 length - 1 个字节、换行符(包含在返回值中)或 EOF(以先到者为准)时,读取结束。如果没有指定长度,它将继续从流中读取,直到到达行尾。

来自: http: //php.net/manual/en/function.fgets.php

于 2013-07-06T19:31:01.920 回答