Have you ever considered using PHP for multi-thread operations?

Source: Internet
Author: User
Have you considered using PHP for multithreading ~ Recently, it has been improved whether the final combination of file content can be read separately by segment for a remote download file in the download class. Tested and feasible ~ This is the local file I tested ~ What I want is how to encapsulate arbitrary local files or remote files in the simplest way! See: & lt ;? Php // has PHP been used for multi-threaded file reading ~
Recently, it has been improved whether the final combination of file content can be read separately by segment for a remote download file in the download class. Tested and feasible ~ This is the local file I tested ~
What I want is how to encapsulate arbitrary local files or remote files in the simplest way! See:
// PHP segment-based file reading. here I only divided two segments. In fact, you can segment the file according to the size of the specified segment ..
$ File = dirname (_ FILE _). '/bullyframework_zend.7z ';

Echo filesize ($ file );

$ Handle = fopen ($ file, "rb ");

$ Contents = fread ($ handle, ceil (filesize ($ file)/2); // read the file combination twice

$ Now_tell = ftell ($ handle); // get the current pointer

$ End = fread ($ handle, filesize ($ file)-$ now_tell); // read the remaining file from the current pointer

Fclose ($ handle );

File_put_contents('test.7z', $ contents. $ end); // write the file to verify whether the file is complete.

Echo"
";

Echo filesize('test.7z ');
?>

------ Solution --------------------
Multipart download requires both parties to support "resumable Upload"

Download tools are generally supported, so what you need to do is the server side. The resumable Upload service implemented by php is roughly as follows:
PHP code
    //   Begin writing headers     header("Cache-Control:");     header("Cache-Control: public");     header("Content-Type: $ctype");     $filespaces = str_replace("_", " ", $filename);     // if your filename contains underscores, replace them with spaces     $header='Content-Disposition: attachment; filename='.$filespaces;     header($header);     header("Accept-Ranges: bytes");          $size = filesize($file);        //   check if http_range is sent by browser (or download manager)        if(isset($_SERVER['HTTP_RANGE'])) {         // if yes, download missing part               $seek_range = substr($_SERVER['HTTP_RANGE'] , 6);         $range = explode( '-', $seek_range);          if($range[0] > 0) { $seek_start = intval($range[0]); }          if($range[1] > 0) { $seek_end  =  intval($range[1]); }                      header("HTTP/1.1 206 Partial Content");         header("Content-Length: " . ($seek_end - $seek_start + 1));         header("Content-Range: bytes $seek_start-$seek_end/$size");      } else {         header("Content-Range: bytes 0-$seek_end/$size");         header("Content-Length: $size");      }        //open the file     $fp = fopen("$file","rb");          //seek to start of missing part        fseek($fp,$seek_start);         //start buffered download     $n = 0;    while(!feof($fp)) {                //reset time limit for big files         echo fread($fp,1024*$speed);      }     fclose($fp);      exit;

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.