- // Basic file operations
- // Fopen ()
- Open a file
- // Open a file in the absolute path, select read-only mode, and return the resource $ handle
- $ Handle = fopen ("D:/lamp/apache2/htdocs/test/file.txt", "r ");
- // Access the file in the root directory of the document and select the read-only mode
- $ Handle = fopen ($ _ SERVER ['document _ root']. "/test/file.txt", "r ");
- // Open a remote file. the http protocol can only be opened in read mode.
- $ Handle = fopen ("http://www.wowsai.com", "r ");
- // Use the FTP protocol to open a remote file. if the FTP server is writable, it can be opened in write mode.
- // $ Handle = fopen ("ftp: // user: password@example.com/file.txt", 'w ');
- // Fclose ()
- Disable open resource type
- // Fwrite ()
- Write content to the file
- $ FileName = "data.txt ";
- // Declare the variable of a file
- // Open the file in write-only mode. If no file exists, create the file and use the program when opening fails.
- $ F_handle = fopen ("data.txt", "w") or die ("Open". $ FileName ."File failed ");
- For ($ I = 0; $ I <10; $ I ++ ){
- // Add content to the file through a loop
- Fwrite ($ f_handle, "again \ n ");
- }
- Fclose ($ f_handle );
- // Close the opened file
- // File_put_contents ()
- Write all data to the specified file at a time
- // Read the file content
- // Fread ()
- Read open files
- // File_get_contents ()
- Read a file into a string
- // Fgets ()
- Returns a row from an opened file.
- // Fgetc ()
- Returns characters from an opened file.
- // File ()
- Read the file into an array.
- // Readfile ()
- Reads a file and outputs it to the output buffer.
- // Feof ()
- Determines whether a file pointer is located at the end of the file.
- // Read the specified number of bytes from the file and save it to a variable.
- $ FileName2 = "data.txt ";
- $ F_hand = fopen ($ fileName2, "r") or die ("file opening failed ");
- $ Contents = fread ($ f_hand, 50 );
- Fclose ($ f_hand );
- Echo $ contents ."
";
- // Read all the content from the file and store it into a variable. each time you read a part, read it cyclically.
- /* $ FileName3 = "D:/lamp/apache2/icons/link.gif ";
- // Save the file name of the binary file to a variable
- $ F3_handle = fopen ($ fileName3, "rb") or die ("file opening failed"); // open the file in read-only mode, and add "B"
- $ F3_contents = "";
- // Declare a string used to save the file content
- While (! Feof ($ f3_handle )){
- // Read the content of the file cyclically and know the end of the file
- $ F3_contents. = fread ($ f3_handle, 1024 );
- // Read 1024 characters each time
- }
- Fclose ($ f3_handle );
- Echo $ f3_contents ;*/
- // Another method for reading all the content of a 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 object length, so as to read all the object content
- Fclose ($ f4_handle );
- Echo $ f4_con ."
";
- // Another method is to read all the content of a file, which is much better than the above method.
- 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 ends with the end of the file
- $ Buffer = fgets ($ f5_handle );
- // Read a row from the file each time
- Echo $ buffer ."
";
- }
- Fclose ($ f5_handle );
- $ F6_handle = fopen ("data.txt", "r") or die ("file opening failed ");
- While (! Feof ($ f6_handle )){
- // Determine whether the pointer ends with the end of the file
- $ Buffer = fgetc ($ f6_handle );
- // Read one character from the object each time
- Echo $ buffer ."
";
- }
- Fclose ($ f6_handle );
- Print_r (file ("data.txt "));
- // Read the file into an array
- Readfile ("data.txt ");
- // Read the content of the file and output it to the browser.
- // Access a remote file
- Make sure that "allow_url_fopen" in php. ini is enabled and that the remote file has access permissions.
- $ Ws_file = fopen ("http://www.wowsai.com", "r") or die ("remote file opening failed ");
- // Open a remote file
- While (! Feof ($ ws_file )){
- $ Ws_line = fgets ($ ws_file );
- If (preg_match ("/(. *) <\/Title>/", $ ws_line, $ res )){
- // Use the regular expression to match the website title
- $ Title = $ res [1];
- Break;
- }
- }
- Fclose ($ ws_file );
- Echo $ title ."
";
- // Move the object pointer
- // 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 opening failed ");
- // Open the file in read-only mode
- Echo ftell ($ fp )."
";
- // Output the pointer position when the file is opened. the default value is 0.
- Echo fread ($ fp, 10 )."
";
- // Read the first 10 characters of the file, and the file pointer has changed.
- Echo ftell ($ fp )."
";
- // After reading the file with 10 characters, the pointer of the file reaches the 10 position
- Fseek ($ fp, 30, SEEK_CUR );
- // Move the object pointer 30 characters backward
- Echo ftell ($ fp )."
";
- // 30 characters after the file is moved, it reaches the position of 40.
- Echo fread ($ fp, 10 )."
";
- // Read the characters between 40 and 50, and the pointer will reach 50
- Fseek ($ fp,-10, SEEK_END );
- // Set the pointer to the last 10th position 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 pointer of the file returns to the beginning, so 0 is output.
- Fclose ($ fp );
- ?>
|