-
- //Basic file operations
- //fopen()
- Open a file
- //Open a file using an absolute path, select read-only mode, and return the resource $handle
- $handle= fopen("D:/lamp/apache2/htdocs/test/file.txt","r");
- //Access files in the document root directory and also select read-only mode
- $handle=fopen($_SERVER[' DOCUMENT_ROOT']."/test/file.txt","r");
- //Open remote files, which can only be opened in read mode using the http protocol
- $handle=fopen("http://www.wowsai. com","r");
- //Use FTP protocol to open remote files. If the FTP server is writable, it can be opened in write mode
- //$handle=fopen("ftp://user:password@example. com/file.txt",'w');
- //fclose()
- Close the open resource type
- //fwrite()
- Write content to the file
- $fileName="data.txt";
- // Declare a file variable
- //Open the file in write-only mode, create it if it does not exist, and pass the program when the opening fails
- $f_handle=fopen("data.txt","w")or die("Open< b>".$fileName."File failed");
- for($i=0;$i<10;$i++){
- //Add content to the file through a loop
- fwrite ($f_handle,"againn");
- }
- fclose($f_handle);
- //Close the open file
- //file_put_contents()
- Write all data to the specified file at once
- //Read the file content
- //fread()
- Read an open file
- //file_get_contents()
- Read a file into a string
- //fgets()
- Return a line from an open file
- //fgetc()
- From an open file Return characters in
- //file()
- Read the file into an array
- //readfile()
- Read a file and output it to the output buffer
- //feof()
- Judge whether a file pointer is at the end of the file At
- //Read the specified number of bytes from the file and store it in a variable
- $fileName2="data.txt";
- $f_hand=fopen($fileName2,"r")or die("Open the file Failed");
- $contents=fread($f_hand,50);
- fclose($f_hand);
- echo $contents."
";
- //Read all the contents from the file and store them in a variable , read part of it each time, read in a loop
- /* $fileName3="D:/lamp/apache2/icons/link.gif";
- //Save the file name of the binary file into a variable
- $f3_handle= fopen($fileName3,"rb")or die("File opening failed"); //Open the file in read-only mode, the mode is added with "b"
- $f3_contents="";
- //Declare one for saving String of file content
- while(!feof($f3_handle)){
- //Loop to read the contents of the file until the end of the file
- $f3_contents.=fread($f3_handle,1024);
- //Read each time 1024 characters
- }
- fclose($f3_handle);
- echo $f3_contents; */
- //Another way to read the entire contents of the file
- $fileName4="data.txt";
- $f4_handle=fopen($fileName4 ,"r")or die("File opening failed");
- $f4_con=fread($f4_handle,filesize($fileName4));
- //Use filesize to get the length of the file, so as to read the entire content of the file
- fclose($f4_handle);
- echo $f4_con."
";
- //Another method to read the entire contents of the file, which has much better performance than the above
- echo file_get_contents("data.txt");
- $f5_handle=fopen("data.txt","r") or die("File opening failed");
- while(!feof($f5_handle)){
- //Determine whether the pointer reaches the end of the file
- $buffer= fgets($f5_handle);
- //Read one line from the file at a time
- echo $buffer."
";
- }
- fclose($f5_handle);
- $f6_handle=fopen("data.txt", "r") or die("File opening failed");
- while(!feof($f6_handle)){
- //Judge whether the pointer reaches the end of the file
- $buffer=fgetc($f6_handle);
- //Every time from Read a character from the file
- echo $buffer."
";
- }
- fclose($f6_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
- //Access remote files
- Make sure "allow_url_fopen" in php.ini is turned on, and make sure The remote file has access permission
- $ws_file=fopen("http://www.wowsai.com","r")or die("Remote file opening failed");
- //Open the remote file
- while(!feof( $ws_file)){
- $ws_line=fgets($ws_file);
- if(preg_match("/(.*)/",$ws_line,$res)){
- //Use Regularly matches the title of the website
- $title=$res[1];
- break;
- }
- }
- fclose($ws_file);
- echo $title."
";
- //Move the pointer of the file
- //ftell()
- Return 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 opening failed");
- //Open the file in read-only mode
- echo ftell($fp)."
";
- //Output the position of the pointer when the file was just opened. The default is 0
- echo fread($fp,10)."
";
- // After reading the first 10 characters of the file, the file pointer has changed
- echo ftell($fp)."
";
- //After reading the first 10 characters of the file, the file pointer has reached position 10
- fseek ($fp,30,SEEK_CUR);
- //Move the file pointer backward 30 characters
- echo ftell($fp)."
";
- //After moving the file 30 characters, it reaches 40 Position
- echo fread($fp,10)."
";
- //Read characters between 40 and 50, the pointer will reach 50
- fseek($fp,-10,SEEK_END);
- // Set the pointer to the 10th position from the bottom of the file
- echo fread($fp,10)."
";
- //Output the last 10 characters of the file
- rewind($fp);
- //Set the file pointer To the beginning of the file
- echo ftell($fp);
- //The file pointer returns to the beginning, so 0
- fclose($fp);
- ?>
-
Copy code
|