PHP Operation files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.