One. PHP read the file to get the file information
1<?PHP2 3 $file _full_path= ' D:/test.txt ';4 if(file_exists($file _full_path)){5 $fp=fopen($file _full_path, ' R ');6 //Set Buffering7 $buffer= ' ';8 $buffer _size=1024;9 $con _str= ' ';Ten One //!feof () indicates that the read continues without the end of the file A while(!feof($fp)) { - $buffer=fread($fp,$buffer _size); - $con _str.=$buffer; the } - - fclose($fp); - $con _str=Str_replace("\ r \ n", ' <br> ',$con _str); + $con _str=Str_replace("\ n", ' <br> ',$con _str); - Echo $con _str; +}Else{ A Echo"<br> file does not exist"; at } -?>
Two. Three ways to read files
1. Mode One
1<?PHP2 3 $file _full_path= "D:/test.txt";4 5 if(file_exists($file _full_path)){6 //Open File7 $fp=fopen($file _full_path, ' R ');8 //get the size of a file9 $file _size=filesize($file _full_path);Ten //Read Content One $con _str=fread($fp,$file _size); A //closing files is important!! - fclose($fp); - //line breaks in Windows are not the same as newline characters in PHP, so replace the $con _str=Str_replace("\ r \ n", ' <br> ',$con _str); - //compatible processing - $con _str=Str_replace("\ n", ' <br> ',$con _str); - Echo $con _str; +}Else{ - Echo' <br> file does not exist '; + } A?>
2. Mode two (suitable for large file reading with buffer buffers)
1<?PHP2 3 $file _full_path= ' D:/test.txt ';4 if(file_exists($file _full_path)){5 $fp=fopen($file _full_path, ' R ');6 //Set Buffering7 $buffer= ' ';8 $buffer _size=1024;9 $con _str= ' ';Ten One //!feof () indicates that the read continues without the end of the file A while(!feof($fp)) { - $buffer=fread($fp,$buffer _size); - $con _str.=$buffer; the } - - fclose($fp); - $con _str=Str_replace("\ r \ n", ' <br> ',$con _str); + $con _str=Str_replace("\ n", ' <br> ',$con _str); - Echo $con _str; +}Else{ A Echo"<br> file does not exist"; at } - -?>
3. Read the content is not a lot of suggestions the third Kind
1<?PHP2 Header(' Content-type:text/html;charset=utf-8 ');3 //The third way to deal with it-simple way4 5 //1. defining file Paths6 $file _full_path= ' D:/test.txt ';7 8 if(file_exists($file _full_path)){9 Ten //File_get_contents did a package processing, the underlying use is still fopen fread. One $con _str=file_get_contents($file _full_path); A //prevent garbled Some people do not seem to write this I was garbled anyway ... - $con _str=Iconv("gb2312", "Utf-8//ignore",$con _str); - the //Replace line break - $con _str=Str_replace("\ r \ n", ' <br> ',$con _str); - $con _str=Str_replace("\ n", ' <br> ',$con _str); - + Echo $con _str; - +}Else{ A Echo' <br> file does not exist '; at } - -?>
PHP Operation files