Php-PHP source code

Source: Internet
Author: User
There are many ways to read files in the php language. Today we are going to talk about three php examples of reading text files, mainly using the fopen, file, file_get_contents functions. There are many ways to read files in the php language. Today we are going to talk about three php examples of reading text files, mainly using the fopen, file, file_get_contents functions.

Script ec (2); script

// Fopen File Reading instance

The Code is as follows:

$ Path named 'a.txt ';
$ Fp = fopen ($ file, "r"); // open the file in read-only mode
While (! (Feof ($ fp )))
{
$ Text = fgets ($ fp); // read a row of the object
Echo $ text;
}

// File_get_contents reads the object

The Code is as follows:

If (file_exists ($ path ))
{
$ Body = file_get_contents ($ path );
Echo $ body; // Input File Content
}
Else
{
Echo "the file does not exist $ path ";
}

// File

Read text files

The Code is as follows:
$ Cbody = file ($ path );


Print_r ($ cbody); // because the files read by file are saved as arrays, print_r is used for output.

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.