PHP file programming (2)

Source: Internet
Author: User

1. File Reading

<? Php // read the file $ file_path = "text.txt"; if (! File_exists ($ file_path) {echo "file does not exist"; exit () ;}// open the file $ fp = fopen ($ file_path, "a + "); // read the file $ content = fread ($ fp, filesize ($ file_path); echo "the file content is: <br/> "; // by default, after the content is output to the webpage, It is not displayed with line breaks, because the web page does not recognize \ r \ n // All \ r \ n => <br/> $ content = str_replace ("\ r \ n ", "<br/>", $ content); echo $ content; fclose ($ fp);?>

2. method 2 for reading files

<? Php // second method of reading files $ file_path = "text.txt"; if (! File_exists ($ file_path) {echo "file does not exist"; exit () ;}$ content = file_get_contents ($ file_path); $ content = str_replace ("\ r \ n ", "<br/>", $ content); echo $ content;?>

3. The third method is to read data cyclically (to deal with large files)

<? Php // The third method for reading data. It reads data cyclically (against large files) $ file_path = "text.txt"; if (! File_exists ($ file_path) {echo "file does not exist"; exit () ;}// open the file $ fp = fopen ($ file_path, "a + "); // defines the number of bytes read each time. $ buffer = 1024; // one side is read. While determining whether it reaches the end of the file while (! Feof ($ fp) {// read data by 1024 bytes $ content = fread ($ fp, $ buffer); echo $ content;} fclose ($ fp);?>

4. File Reading application: When we connect to the database, we can configure the specified data to a file, and then obtain information in real time when PHP is running.

Db. ini file

host=127.0.0.1user=rootpwd=rootdb=test

Get File

<? Php $ arr = parse_ini_file ("db. ini "); echo" <pre> "; print_r ($ arr); echo" </pre> "; echo $ arr ['host']; // connect to the database $ conn = mysql_connect ($ arr ['host'], $ arr ['user'], $ arr ['pwd']); if (! $ Conn) {echo "error";} echo "OK";?>

 

 

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.