What is the need to note about PHP fread reading files?

Source: Internet
Author: User
Tags fread php source code
The PHP fread () function is used to read files (which can be used safely in binary files). This article introduces you to FreadRead file example, and a few points to note when using this function to read a file, a friend you need can refer to.

Introduction to PHP fread functions

Grammar

Fread (File,length)

Parameters

File required. Specifies to read open files.

Length required. Specifies the maximum number of bytes to read.

Description

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

Returns the read string if an error returns false.

Fread () Example:

Read 10 bytes from a file:

<?php$file = fopen ("Test.txt", "R"), Fread ($file, "ten"); fclose ($file);? >

Some points to be aware of when using PHP fread

1, Fread read write to large file error resolution

The following method resolves the problem of reading large files if the maximum memory usage value set in php.ini is exceeded when using fread to read the file:

<?   Set_time_limit (0);//Set Script execution time   is infinite $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 new file   $handle =fopen ($filename, "a");//write open to point the file pointer to the end of the file. If the file does not exist, try to create the while   (!feof ($fp)) {//test file pointer to the location where the file ends     $content =fread ($fp, 1024x768);     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 open $content=fread ($handler, 1024x768); Echo $content;? >

Fread is returned as a string, how does it recognize the encoding used by a.txt, to ensure that it is not garbled?

The character encoding is not recognized in the file operation of the following version of PHP7.0.

Just by byte data output, if and the PHP source code file and output HTML character encoding consistent can be displayed correctly.

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 empty character}

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

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.