PHP fread Read file Considerations _php Tips

Source: Internet
Author: User
Tags fread memory usage

Introduction to PHP fread function

string fread (int handle, int length)

Fread () reads up to length bytes from the file pointer handle. The function stops reading a file when it reads up to a maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or (after opening a user-space stream), 8,192 bytes have been read.

Fread () instance:

<?php
  $file = "Data.txt";
  $fh = fopen ($file, "RT");
  $userdata = Fread ($fh, FileSize ($file));
  Fclose ($FH);
? >
 

Some points to be noted in PHP using Fread

1, Fread read Write a large file error resolution

When you use Fread to read a file, you are prompted for an error if you exceed the maximum memory usage value set in PHP.ini, and the following method resolves the problem of reading a large file:

? 
  Set_time_limit (0)//Set script execution time infinitely long 
  $flie = "Flexbuilder_linux_install_a5_112409.bin";//large file exceeds memory configuration in php.ini 
  $ Fp=fopen ($flie, "R"); 
  $content = ""; 
  $filename = "123.bin";//Save as a new file 
  $handle =fopen ($filename, "a"),//write open, point the file pointer to the end of the file. If the file does not exist, try to create a while 
  (!feof ($fp)) {//test file pointer to the location of the end of the file 
    $content =fread ($FP, 1024); 
    Fwrite ($handle, $content); 
  } 
  Fclose ($FP); 
  Fclose ($handle); 
  echo "Data successfully written to file"; 
? >
 

2, PHP fread () is how to identify the file encoding

<?php
$handler =fopen (' a.txt ', ' RB ')//Binary mode opens
$content =fread ($handler, 1024);
echo $content;
? >

Fread as a string return, that it is how to identify a.txt use of the encoding method, to ensure that not garbled it?

PHP7.0 the following version of the file operation does not recognize character encoding.

Just by byte data output, if the PHP source file and output HTML character encoding consistent with the correct display.

3, Fread read the file will always be more than one empty character

$fileSize = FileSize ($filePath);
$handle = fopen ($filePath, "RB"); 
while (!feof ($handle)) {
    var_dump (fread ($handle, $fileSize));
    Will output one more blank character
}

When we use the above code to read files, there are sometimes more than one empty character. This is because you are the Windows platform, the file is the text to open the stored content, the end will have a special byte identification file end, you use RB open nature can read the last special byte. Open it with R, Fgets read it.

Through this article hope to help everyone, thank you for your support to this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.