Home > Article > Backend Development > PHP read file content code (txt, js, etc.)_PHP tutorial
/*
Author: bjf;
Application: Read file content;
*/
function read_file_content($FileName)
{
//open file
$fp=fopen($FileName,"r");
$data="";
while(!feof($fp))
{
//read the file
$data.=fread($fp,4096);
}
//close the file
fclose($fp);
//delete the file
//unlink ($FileName);
//return the content from the file
echo $data;
}
read_file_content("a.html")
?>
fread and fgets The difference
fread: Calculate the length in bytes, read data according to the specified length and number of times, and stop after encountering the end or completing the specified length of reading.
fgets: Read the entire line, and encounter a carriage return Line break or end stop. Used in text mode.