Home Backend Development PHP Tutorial 有没有思忖过用PHP来做多线程操作

有没有思忖过用PHP来做多线程操作

Jun 13, 2016 pm 01:09 PM
file header quot range Seek

有没有考虑过用PHP来做多线程操作~
最近完善下载类中一个远程下载文件 考虑是不是可以分别分段读取文件内容 最后组合。经测试可行哦~ 这是我测试的本地文件~
我想的是咋个可以最简单封装针对任意本地文件 or远程文件 进行操作! 请看:
//PHP分段读取文件 这里我只分了两段 实际上可以通过文件大小 按照预定段大小 进行分段..
$file = dirname(__FILE__).'/bullyframework_zend.7z';

echo filesize($file);

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

$contents = fread($handle, ceil(filesize($file)/2)); //分两次读取文件组合

$now_tell = ftell($handle); //获取当前指针

$end = fread($handle, filesize($file) - $now_tell); //从当前指针处读取剩余文件

fclose($handle);

file_put_contents('test.7z', $contents.$end); //写入文件 验证文件是否完整

echo "
";

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

------解决方案--------------------
分块下载需要双方都支持“断点续传”

一般下载工具都支持,所以你需要做的是服务器端。php实现的断点续传服务大致如下

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; <div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Seek Thermal releases Nano 200 and Nano 300 thermal imaging cameras - handy for spotting cooling problems in overclocked PCs Seek Thermal releases Nano 200 and Nano 300 thermal imaging cameras - handy for spotting cooling problems in overclocked PCs Aug 12, 2024 pm 09:50 PM

Seek Thermal has released the improved Nano 200 and Nano 300 thermal imaging cameras for Android and iOS phones. The infrared cameras see heat and help PC modders detect cooling and heating problems quickly when overclocking CPUs and GPUs. A traditio

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

What does linux header mean? What does linux header mean? Jul 18, 2023 pm 03:34 PM

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

How does SpringBoot pass parameters in the Header through Feign calls? How does SpringBoot pass parameters in the Header through Feign calls? May 16, 2023 pm 08:38 PM

[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

Use java's File.getParentFile() function to get the parent directory of the file Use java's File.getParentFile() function to get the parent directory of the file Jul 27, 2023 am 11:45 AM

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

See all articles