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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条