PHP file operations, multiline sentence reading, file () function, file_get_contents () function, file_put_contents () function, is_file, statistics of website pv (traffic volume), file copy, rename the file, delete the file unlink,

Source: Internet
Author: User

PHP file operations, multiline sentence reading, file () function, file_get_contents () function, file_put_contents () function, is_file, statistics of website pv (traffic volume), file copy, rename the file, delete the file unlink,

Add UTF-8 to php:

1 header ("Content-type: text/html; charset = 'utf-8 '");

File Operation steps:

1. Create a file.txt folder under the same directory

2. open the file

1 $ res = fopen ("file.txt", "r"); // open the file path, which is a resource and needs further processing; // r indicates read-only.

3. Read files

$ Str = fread ($ res, 300); // The second parameter is the read length (3 for each Chinese character) $ str = fread ($ res, filesize ("file.txt"); // filesize: Read the file size. // select echo $ str for either of the preceding two values;

4. close the file

Fclose ($ res); // after reading, close the resource

Reading multiple sentences:

1. Set the text to read/write and write multiple lines of text.

2. Read one or multiple rows

1 // open the file
2 $ res2 = fopen ("file.txt", "r"); 3 $ str2 = fgets ($ res2 ); // Line 4 echo $ str2. "<br>"; // line feed 5 $ str2 = fgets ($ res2); 6 echo $ str2; // The second line appears here
7 // use the while loop to display all of them (as shown below)
8 while ($ str2 = fgets ($ res2 )){
9 echo $ str2. "<br> ";
10}
11 // close the file
12 fclose ($ res2 );

The file () function reads a file into an array, and each element is separated by a line break:

1 $arr = file("file.txt");2 print_r($arr);3 echo "<table border = 1>";4 for($i = 0;$i < count($arr);$i++) {5     echo"<tr><td>".$arr[$i]."</td></tr>";6 }7 echo "</table>";

The file_get_contents () function reads the file content to a string (cross-origin reading is supported ):

1 $ str4 = file_get_contents ("http://www.jd.com"); // cross-domain 2 echo $ str4;

* ** The file_put_contents () function writes a string to a file, just like calling the fopen (), fwrite (), and fclose () functions in turn;

1 $ bol = file_put_contents ("file.txt", "I love you"); // You can overwrite the previous content with 2 echo $ bol;

* ** Is_file: determines whether a file exists.

1 $ bol = is_file ("file3.txt"); // check whether there is 2 echo $ bol in file3.txt;

Use the preceding annotation (***) to calculate the website pv (Traffic Volume ):

1 // first determine whether there is any statistical file 2 if (is_file ("pv.txt ")) {// 3 // get the value 4 $ res = file_get_contents ("pv.txt") in the file; 5 // accumulate 6 $ res + = 1; 7 // the added value of the class is stored in 8 file_put_contents ("pv.txt", $ res); 9 // output pv count 10 echo file_get_contents ("pv.txt "); 11} else {// file 12 without statistics // create a file and give the file an initial value of 13 file_put_contents ("pv.txt", 1 ); 14 // output the current pv: 115 echo file_get_contents ("pv.txt"); 16}

Copy

 copy("pv.txt","pv2.txt");

Rename a file

rename("pv2.txt","pv5.txt");

Delete file unlink

 unlink("pv5.txt");

 

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.