PHP Five ways to read file contents

Source: Internet
Author: User
Tags fread php read file
PHP Five ways to read file contents

Share the Five ways PHP reads the contents of a file: All right, when you're done, you'll find that the files are not closed. In practical application, please pay attention to closing fclose ($FP);
--

PHP Read File contents:

-----The first Method-----fread ()--------

<?php$file_path = "Test.txt", if (file_exists ($file _path)) {$fp = fopen ($file _path, "R"); $str = Fread ($fp, FileSize ($ File_path));//Specify the read size, where the entire contents of the file are read echo $str = Str_replace ("\ r \ n", "
", $str);}? >

--------the second method------------

<?php$file_path = "Test.txt", if (file_exists ($file _path)) {$str = file_get_contents ($file _path);// Reads the entire file contents into a string $str = Str_replace ("\ r \ n", "
", $str); echo $str;}? >

-----The third method------------

<?php$file_path = "Test.txt", if (file_exists ($file _path)) {$fp = fopen ($file _path, "R"); $str = ""; $buffer = 1024;// Reads 1024 bytes at a time (!feof ($fp)) {//loops through read until the entire file $str is read. = Fread ($fp, $buffer);} $str = Str_replace ("\ r \ n", "
", $str); echo $str;}? >

-------the fourth method of--------------

<?php$file_path = "Test.txt", if (file_exists ($file _path)) {$file _arr = file ($file _path); for ($i =0; $i
 
  
";} /*foreach ($file _arr as $value) {echo $value. "
";} */}?>

----The fifth method of--------------------

<?php$file_path = "Test.txt", if (file_exists ($file _path)) {$fp = fopen ($file _path, "R"); $str = ""; while (!feof ($fp)) { $str. = Fgets ($fp);//read by line. If Fgets does not write the length parameter, the default is to read 1k. } $STR = Str_replace ("\ r \ n", "
", $str); echo $str;}? >

The above content for you to share the PHP read the contents of the five ways, I hope you like it.

The above describes the PHP five ways to read the contents of the file, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.