Below we use several examples to summarize the use of the phpfopen function to implement file read and write operations. if you need to learn, refer. For a simple reference, refer to the fopen function fopen () function to open a file or URL. If opening fails, this function returns FALSE.
Below we use several examples to summarize the use of the php fopen function to implement file read and write operations. if you need to learn, refer.
For a simple reference, see the fopen function fopen () function to open a file or URL.
If opening fails, this function returns FALSE.
Syntax
Fopen (filename, mode, include_path, context)
The instance code for creating a file is as follows:
-
- If (! File_exists ("test.txt") {// if the file does not exist (the default value is in the current directory)
- $ Fh = fopen ("test.txt", "w ");
- Fclose ($ fh); // close the file
- }
- ?>
The code for modifying and editing a robots file is as follows:
-
- Function get_txt ($ robots_file)
- // Define the function. the content is included in {}.
- {
- If (file_exists ($ robots_file ))
- // If the object exists, read the content.
- {
- $ Fp = @ fopen ($ robots_file, "r ");
- // R is the abbreviation of read, which indicates reading. open a file in read-only mode.
- If ($ fp ){
- While (! Feof ($ fp) {// if it is not at the end of the file
- $ Robots_txt = fgets ($ fp, 4096); // read data row by row
- $ Robots_all = $ robots_all. $ robots_txt; // save the data to $ robots_all.
- }
- Return ($ robots_all); // return all content
- Fclose ($ fp); // close the file
- }
- }
- }
- Function put_txt ($ robots_txt)
- {
- $ Fp = fopen ("robots.txt", "w ");
- // W is the abbreviation of write, which indicates the meaning of writing. open the file as a write.
- Fputs ($ fp, $ robots_txt );
- // Output text to a file
- Fclose ($ fp );
- }
- ?>
-
- $ Edit = $ _ GET ["edit"];
- $ Txt = $ _ POST ["txt"];
- $ Get_txt = get_txt ("robots.txt ");
- // Call the defined function to open the robots file.
- If ($ edit = "write ")
- {
- Put_txt ($ txt );
- Echo "success save and return ";
- }
- Else
- {
- Echo "returned results of successful reading of robots.txt ";
- }
- ?>
-
- If ($ edit = "")
- {
- ?>
-
- }
- ?>
Read the data in the counter.txt file through php and save it to the text document.
Create a counter. php document and enter the following code. Unlike ASP, single-line comments in PHP are implemented with // or #, and multi-line comments are implemented:
The instance code is as follows:
-
- Function get_hit ($ counter_file)
- // Define the function. the content is included in {}. it should be similar to the C language.
- {
- $ Count = 0;
- // Returns the counter to zero. add the $ sign before the variable in Php.
- If (file_exists ($ counter_file ))
- // If the counter file exists, read the content
- {
- $ Fp = fopen ($ counter_file, "r ");
- // R is the abbreviation of read, which indicates reading. open a file in read-only mode.
- $ Count = 0 + fgets ($ fp, 20 );
- /* Assign the value of the first 20 digits to the count variable. because the fgets () function reads strings, you must convert them to integers by adding + 0,
- This is different from ASP. Strings in ASP can be directly computed with integers without conversion .*/
- Fclose ($ fp );
- // Close the file
- }
- $ Count ++;
- // Increase the count, which is very similar to C.
- $ Fp = fopen ($ counter_file, "w ");
- // W is the abbreviation of write, which indicates the meaning of writing. open the file as a write.
- Fputs ($ fp, $ count );
- // Output the count value to the file
- Fclose ($ fp );
- Return ($ count );
- // Return the count value
- }
- ?>
-
- $ Hit = get_hit ("counter.txt ");
- // Call the just-defined counter to process the counter.txt document and assign the result to the hit variable.
- Echo "you are the first"." $ Hit "."Visitor! ";
- // The difference between output result. PHP and ASP is that the connection character of ASP is "&", while that of Php is ".".
- ?>
Also insert this file in the PHP document to be called: