doujiong2533 2014-10-30 15:30
浏览 22
已采纳

在PHP中从服务器发送文件块

I am developing an embedded device which has a simple miro-controller with limited memory. This device will request a file from a server by sending a HTTP (or HTTPS) GET method request to the server. There will be a PHP script which in the server responsible to send the file. Now the PHP script will only send the file continuously to the embedded device. However as the embedded device is not fast enough and do not have enough memory to store the whole file before processing it. I want the PHP script to only sending a chunk of the file in each HTTP GET request. I think it is good that the size of the chunk is determined by the variable in the GET request. And in each chunk it will add a header describing the size, the sequence number, and CRC check of that chunk.

I am a newbie on PHP script. Could you help to guild me to write the PHP script? An example would be really appreciated.

Thank you very much.

  • 写回答

1条回答 默认 最新

  • douxiyi2418 2014-10-30 16:20
    关注

    I think that your script PHP could read the file and take the chunk you want:

    $filename = "YOURFILE.txt";
    $chunk_length = 1024;  // 1024 chars will be sent
    $sequence_number = $_GET['sequence'];
    if ($sequence_number>0){
        $position = $sequence_number * $chunk_length;
    }
    else {
        $position = 0;
    }
    $content = file_get_contents($filename);
    $data = substr($content, $position, $chunk_length);
    header('size:'.strlen($data));
    header('sequence_number:'.sequence_number);
    header('CRC:'.crc32($data));
    echo $data;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗