Php 这个get_text函数做什么?
回答这个问题做得很好: 但是他创建了一个我完全不理解的函数,并希望得到某种解释。例如,随机8192数字是什么Php 这个get_text函数做什么?,php,Php,回答这个问题做得很好: 但是他创建了一个我完全不理解的函数,并希望得到某种解释。例如,随机8192数字是什么 function get_text($filename) { $fp_load = fopen("$filename", "rb"); if ( $fp_load ) { while ( !feof($fp_load) ) { $content .= fgets($fp_load, 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;
}
}
它加载路径在
$filename
中的文件并返回其内容。8192不是随机的。这意味着以8kb的块读取文件
只要文件未完全读取,
while
循环就会运行。每次迭代都会将文件的最新8kb添加到函数末尾返回的$content
它加载文件的数据
例如,随机8192数字是什么?
当读取了长度为-1字节或换行符(其中
包含在返回值中),或EOF(以先到者为准)。
如果没有指定长度,它将一直从流中读取,直到
它到达了终点线
要分解它:
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:
读取长度为-1字节、换行符(包含在返回值中)或EOF(以先到者为准)时,读取结束。如果未指定长度,它将一直从流中读取,直到到达行的末尾
from:在
fopen()
call上不要将$filename
放在引号中。这没什么意义,我只是用了Panique用过的东西(如前所述),不管用哪种方式都可以