Notes for php fread file reading,

Source: Internet
Author: User

Notes for php fread file reading,

Introduction to php fread Functions

String fread (int handle, int length)

Fread () reads a maximum of length bytes from the file pointer handle. This function can be read by a maximum of length bytes, or when it reaches the EOF, or (for network streams) when a package is available, or (after opening the user space Stream) when 8192 bytes are read, the system stops reading files.

Fread () instance:

<?php  $file = "data.txt";  $fh = fopen($file, "rt");  $userdata = fread($fh, filesize($file));  fclose($fh);?> 

Notes for using fread in php

1. Solutions to fread errors in reading and writing large files

When you use fread to read files, if the maximum memory usage value set in php. ini is exceeded, an error is prompted. The following method solves the problem of reading large files:

<? Set_time_limit (0); // set the script execution time to an infinite length $ flie = "flexbuilder_linux_install_a5_112409.bin"; // The large file exceeds php. memory configuration in ini $ fp = fopen ($ flie, "r"); $ content = ""; $ filename = "123.bin "; // Save the new file $ handle = fopen ($ filename, "a"); // open the file by writing the pointer to the end of the file. If the object does not exist, try to create the while (! Feof ($ fp) {// test whether the file pointer has reached the end of the file $ content = fread ($ fp, 1024); fwrite ($ handle, $ content );} fclose ($ fp); fclose ($ handle); echo "data is successfully written to the file";?>

2. How does php fread () Identify file encoding?

<? Php?handler=fopen('a.txt ', 'rb') // enable $ content = fread ($ handler, 1024) in binary mode; echo $ content;?>

Freadreturns in the form of strings. How can we identify the encoding method used by a.txt to ensure no garbled characters?

The file operations in versions earlier than PHP7.0 do not recognize character encoding.

Only output by byte data. If it is consistent with the character encoding of the php source code file and the output html, it can be correctly displayed.

 3. fread will always read an empty character

$ FileSize = filesize ($ filePath); $ handle = fopen ($ filePath, "rb"); while (! Feof ($ handle) {var_dump (fread ($ handle, $ fileSize); // an empty character is output once more}

When we use the above Code to read a file, there is sometimes an extra null character. This is because you are a WINDOWS platform, and files are stored in text. A special byte mark file ends at the end of the file. You can use rb to open the file and read the last special byte. Open it with r. Read it with fgets.

I hope this article will help you. Thank you for your support for 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.