- $ Filename = "/usr/local/something.txt ";
- $ Handle = fopen ($ filename, "r"); // when reading binary files, set the second parameter to 'RB'
-
- // Obtain the file size through filesize and read the entire file to a string
- $ Contents = fread ($ handle, filesize ($ filename ));
- Fclose ($ handle );
- ?>
-
If the file to be read is not a local common file, but a remote file or stream file, this method cannot be used, because filesize cannot obtain the size of these files. In this case, you need to use the returned values of feof () or fread () to determine whether the object has been read to the end. For example:
- $ Handle = fopen ('http: // bbs.it-home.org ', 'r ');
- $ Content = '';
- While (! Feof ($ handle )){
- $ Content. = fread ($ handle, 8080 );
- }
- Echo $ content;
- Fclose ($ handle );
- ?>
-
Or:
- $ Handle = fopen ('http: // bbs.it-home.org ', 'r ');
- $ Content = '';
- While (false! = ($ A = fread ($ handle, 8080) {// Return false to indicate that the object has been read to the end.
- $ Content. = $;
- }
- Echo $ content;
- Fclose ($ handle );
- ?>
-
2. fgets method string fgets (int $ handle [, int $ length]) fgets () reads a row from the file pointed to by handle and returns a string with a maximum length of 1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes.
- $ Handle = fopen ('./file.txt', 'r ');
- While (! Feof ($ handle )){
- Echo fgets ($ handle, 1024 );
- }
- Fclose ($ handle );
- ?>
-
Note: The length parameter is optional since PHP 4.2.0. if ignored, the row length is assumed to be 1024. Starting from PHP 4.3, ignoring length will continue to read data from the stream until the row ends. If most of the rows in the file are larger than 8 kB, it is more effective to specify the length of the maximum row in the script to use resources. Starting from PHP 4.3, this function can be safely used for binary files. Earlier versions do not work. 3. the fgetss method string fgetss (resource $ handle [, int $ length [, string $ allowable_tags]) has the same function as fgets, however, fgetss will try to remove any HTML and PHP tags from the read text. you can use the optional third parameter to specify which tags will not be removed.
- $ Handle = fopen ('./file.txt', 'r ');
- While (! Feof ($ handle )){
- Echo fgetss ($ handle, 1024 ,'
');
- }
- Fclose ($ handle );
- ?>
4. filearray file (string $ filename [, int $ use_include_path [, resource $ context]) reads the file content into an array, each of which corresponds to a row in the file, including line breaks. You can use the rtrim () function to filter line breaks when no line terminator is required.
- $ A = file ('./file.txt ');
- Foreach ($ a as $ line => $ content ){
- Echo 'line'. ($ line + 1). ':'. $ content;
- }
- ?>
5. readfile method int readfile (string $ filename [, bool $ use_include_path [, resource $ context]) reads a file and writes it to the output buffer. Returns the number of bytes read from the file. If an error is returned, FALSE is returned, and the error message is displayed unless called in the form of @ readfile.
- $ Size = readfile ('./file.txt ');
- Echo $ size;
- ?>
12. Next page |