[PHP Learning Tutorial]003. High-speed read and write big data "binary" file, do not need to request large memory (Byte Block)

Source: Internet
Author: User
Tags fread

Introduction: Read and write large "binary" files, do not need to apply very large memory (fopen, Fread, fwrite, fclose)! To reduce revenue and increase speed!

Tell yourself once a day, "I'm really good" ....

Speed reading and writing large files, in the actual work process in fact, it must be a lot of people have this experience-you know, if you use Notepad (Notepad) to open 10M text file, it will be stuck to no response, but if you use sublime or notepad++ to open instantly.

Do not expand the talk, the interface is simple, many say useless, directly on the code.

Function

<?PHP/** Read and write the sophomore file, do not need to request a lot of memory * only read the content to create the file * Guaranteed directory writable * @param string $srcPath source file path * @param string $dstPath destination file path * @return BOOL*/functionFetch_big_file ($srcPath,$dstPath) {    //set script execution time to be infinitely long    Set_time_limit(0); if(!$FPSRC=fopen($srcPath, "RB")) {        return false; }    //Write file-is it open?     $isWriteFileOpen=false;  Do {        //8*1024 bytes per read        $data=fread($FPSRC, 8192); if(!$data) {             Break; } Else if(!$isWriteFileOpen) {            //The first time the file is read and there is content to create the file            $fpDst=fopen($dstPath, "WB"); $isWriteFileOpen=true; fwrite($fpDst,$data); } Else {            //Write            fwrite($fpDst,$data); }    }  while(true); fclose($FPSRC); fclose($fpDst); return true;}//examples of features:$srcPath= ' D:/php/data/eclipse-jee-kepler-r-win32-x86_64.pdf ';$dstPath= ' Z:/reslibcovertingfiles/eclipse-jee-kepler-r-win32-x86_64.pdf '; Fetch_big_file ($srcPath,$dstPath);Echo' Success ';

Crossing, if you are wrong, please point it out!

To be continued .....

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4794186.html

[PHP Learning Tutorial]003. High-speed read and write big data "binary" file, do not need to request large memory (Byte Block)

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.