Reputation: 746
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.
Upvotes: 0
Views: 405
Reputation: 672
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;
Upvotes: 1