- $filename = "/usr/local/something.txt";
- $handle = fopen ($filename, "R");//The second parameter needs to be set to ' RB ' when reading a binary file
- Get the file size by FileSize and read the entire file into a string
- $contents = Fread ($handle, FileSize ($filename));
- Fclose ($handle);
- ?>
Copy CodeYou cannot use this method if the file you are reading is not a local normal file, but a remote file or a stream file, because FileSize cannot get the size of these files. At this point, you need to determine whether the end of the file has been read by the return value of feof () or fread (). For example:
- $handle = fopen (' http://bbs.it-home.org ', ' R ');
- $content = ";
- while (!feof ($handle)) {
- $content. = Fread ($handle, 8080);
- }
- Echo $content;
- Fclose ($handle);
- ?>
Copy CodeOr:
- $handle = fopen (' http://bbs.it-home.org ', ' R ');
- $content = ";
- while (false! = ($a = fread ($handle, 8080))) {//returns false to indicate that the end of the file has been read
- $content. = $a;
- }
- Echo $content;
- Fclose ($handle);
- ?>
Copy Code2. Fgets method string fgets (int $handle [, int $length]) fgets () reads a line from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). If length is not specified, the default is 1 K, or 1024 bytes.
- $handle = fopen ('./file.txt ', ' R ');
- while (!feof ($handle)) {
- Echo fgets ($handle, 1024);
- }
- Fclose ($handle);
- ?>
Copy CodeDescription: The length parameter becomes optional from PHP 4.2.0, and if omitted, the line is assumed to be 1024. starting with PHP 4.3, ignoring length will continue to read data from the stream until the end of the line. If most of the rows in the file are larger than 8KB, specifying the length of the largest row in the script is more efficient with resources. Starting with PHP 4.3 This function can be used safely in binary files. The earlier versions were not. 3. The Fgetss method string Fgetss (Resource $handle [, int $length [, String $allowable _tags]] is the same as fgets, but FGETSS attempts to remove from the text read any HTML and PHP tags, you can use the optional third parameter to specify which tags are not removed.
- $handle = fopen ('./file.txt ', ' R ');
- while (!feof ($handle)) {
- Echo fgetss ($handle, 1024, '
');
- }
- Fclose ($handle);
- ?>
Copy Code4. Filearray file (string $filename [, int $use _include_path [, Resource $context]]) reads the contents of the files into an array, each item of the array corresponds to a line in the file, including the newline character. You can use the RTrim () function to filter line breaks when you don't need a line terminator.
- $a = File ('./file.txt ');
- foreach ($a as $line = = $content) {
- Echo ' line '. ($line + 1). ': ' $content;
- }
- ?>
Copy Code5. ReadFile method int ReadFile (string $filename [, bool $use _include_path [, Resource $context]]) reads into a file and writes to the output buffer. Returns the number of bytes read from the file. If an error returns false and the error message is displayed unless it is called in the form of @readfile ().
- $size = ReadFile ('./file.txt ');
- Echo $size;
- ?>
Copy Code1 2 Next last page |