- Basic operation of the file
- fopen ()
- Open a file
- Open a file with an absolute path, select Read-only mode, and return the resource $handle
- $handle =fopen ("D:/lamp/apache2/htdocs/test/file.txt", "R");
- Access files under the root of the document, and also select Read-only mode
- $handle =fopen ($_server[' document_root '). " /test/file.txt "," R ");
- Open a remote file and use the HTTP protocol to open it in a read-only manner
- $handle =fopen ("http://www.wowsai.com", "R");
- Open a remote file using the FTP protocol, if the FTP server is writable, you can open it in write mode
- $handle =fopen ("Ftp://user:password@example.com/file.txt", ' W ');
- Fclose ()
- To close an open resource type
- Fwrite ()
- Writing content to a file
- $fileName = "Data.txt";
- Declaring a variable for a file
- Open a file in a write-only manner, not created, and passed through the program when the open fails
- $f _handle=fopen ("Data.txt", "w") or Die ("open ". $fileName. " File failed ");
- for ($i =0; $i <10; $i + +) {
- Add content to a file through a loop
- Fwrite ($f _handle, "again\n");
- }
- Fclose ($f _handle);
- Close an open file
- File_put_contents ()
- Writes all data to the specified file at once
- Read File contents
- Fread ()
- Read an open file
- File_get_contents ()
- To read a file into a string
- Fgets ()
- Returns a row from an open file
- FGETC ()
- Returning characters from an open file
- File ()
- To read a file into an array
- ReadFile ()
- Reads a file and outputs it to the output buffer
- Feof ()
- Determines whether a file pointer is at the end of the file
- Reads the contents of a specified number of bytes from a file into a variable
- $fileName 2= "Data.txt";
- $f _hand=fopen ($fileName 2, "R") or Die ("File open failed");
- $contents =fread ($f _hand,50);
- Fclose ($f _hand);
- echo $contents. "
";
- Reads everything from a file into a variable, reads one part at a time, loops through
- /* $fileName 3= "d:/lamp/apache2/icons/link.gif";
- Save the file name of the binaries in a variable
- $f 3_handle=fopen ($fileName 3, "RB") or Die ("File open failed"); Open the file in read-only mode with "B" added
- $f 3_contents= "";
- Declares a string to hold the contents of the file
- while (!feof ($f 3_handle)) {
- Loop through the contents of the file, knowing the end of the file
- $f 3_contents.=fread ($f 3_handle,1024);
- Reads 1024 characters at a time
- }
- Fclose ($f 3_handle);
- echo $f 3_contents; */
- Another way to read the entire contents of a file
- $fileName 4= "Data.txt";
- $f 4_handle=fopen ($fileName 4, "R") or Die ("File open failed");
- $f 4_con=fread ($f 4_handle,filesize ($fileName 4));
- Use FileSize to get the length of the file so that it reads the entire contents of the file
- Fclose ($f 4_handle);
- echo $f 4_con. "
";
- Another way to read the entire contents of a file is much better than the performance above
- Echo file_get_contents ("Data.txt");
- $f 5_handle=fopen ("Data.txt", "R") or Die ("File open failed");
- while (!feof ($f 5_handle)) {
- Determine if the pointer is to the end of the file
- $buffer =fgets ($f 5_handle);
- Every time a row is read from a file
- echo $buffer. "
";
- }
- Fclose ($f 5_handle);
- $f 6_handle=fopen ("Data.txt", "R") or Die ("File open failed");
- while (!feof ($f 6_handle)) {
- Determine if the pointer is to the end of the file
- $buffer =fgetc ($f 6_handle);
- Each time a character is read from a file
- echo $buffer. "
";
- }
- Fclose ($f 6_handle);
- Print_r (File ("Data.txt"));
- Read the file into an array
- ReadFile ("Data.txt");
- Read the contents of the file directly and output it to the browser
- accessing remote Files
- Ensure that "Allow_url_fopen" in PHP.ini is turned on and that the remote file has access rights
- $ws _file=fopen ("http://www.wowsai.com", "R") or Die ("Remote File Open failed");
- Open a remote file
- while (!feof ($ws _file)) {
- $ws _line=fgets ($ws _file);
- if (Preg_match ("/(. *) <\/title>/", $ws _line, $res)) {
- Use regular match titles for Web sites
- $title = $res [1];
- Break
- }
- }
- Fclose ($ws _file);
- echo $title. "
";
- Move a pointer to a file
- Ftell ()
- Returns the current position of the pointer
- Fseek ()
- Move the pointer to the specified position
- Rewind ()
- Move the pointer to the beginning of the file
- $FP =fopen ("Data.txt", "R") or Die ("File open failed");
- Open a file with read-only mode
- Echo Ftell ($fp). "
";
- Outputs the position of the pointer when the file was just opened, default is 0
- Echo fread ($FP, 10). "
";
- Read the first 10 characters of a file, the file pointer has changed
- Echo Ftell ($fp). "
";
- After reading 10 characters of the file, the pointer to the file is in the 10 position
- Fseek ($fp, 30,seek_cur);
- Move the pointer of a file backwards by 30 characters
- Echo Ftell ($fp). "
";
- The file moved 30 characters, to the 40 position
- Echo fread ($FP, 10). "
";
- Reads a character from 40 to 50, and the pointer moves to 50
- Fseek ($fp, -10,seek_end);
- Set the pointer to the 10th of the file's penultimate position
- Echo fread ($FP, 10). "
";
- The last 10 characters of the output file
- Rewind ($FP);
- Set the file pointer to the beginning of the file
- Echo Ftell ($FP);
- The pointer to the file goes back to the beginning, so it outputs 0
- Fclose ($FP);
- ?>
Copy Code
|