将数据串组合成一个文件
Combine strings of data to make a file
我目前正在使用一个 API,它提供了一个函数 "GetFileChunk",可以使用 pos 和 len 提取文件的一部分。没有文档,所以我正在试验并尝试先将文件数据拉入数组,然后将其写入文件。
到目前为止,我已经将数据读入数组,但我不确定如何将其写出。我试过 file_put_contents() 但这只是将行写为文本而不是从字节数据创建文件。
$fm = new FileManager();
$data = array();
$pos = 0;
$len = 32;
//while (($chunk = $fm->getFileChunk('test.cfg', $pos, $len)) != '')
for ($i = 0; $i <= 500; $i++)
{
$result = $fm->getFileChunk('test.cfg', $pos, $len);
$chunk = $result->result;
if ($chunk != '')
{
array_push($data, $chunk);
}
$pos += $len;
}
var_dump($data);
file_put_contents("test.cfg", $data);
这是我之后转储数组时的输出结果...
array (size=49)
0 => string 'I0FNUCBDb25maWd1cmF0aW9uIEZpbGUNCiMjIyMjIyM=' (length=44)
1 => string 'IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIw0KI0RPIE4=' (length=44)
2 => string 'T1QgRURJVCBXSElMRSBBTVAgSVMgUlVOTklORw0KIyM=' (length=44)
3 => string 'IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjDQo=' (length=44)
4 => string 'DQojIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM=' (length=44)
5 => string 'IyMNCiMgU2VjdXJpdHkNCiMjIyMjIyMjIyMjIyMjIyM=' (length=44)
6 => string 'IyMjIyMjIyMjIyMjIyMjIw0KU2VjdXJpdHkuRXhlY1A=' (length=44)
7 => string 'YXRoPUV4ZWMNClNlY3VyaXR5LkFsbG93Q29uY3VycmU=' (length=44)
8 => string 'bnRTZXNzaW9ucz1GYWxzZQ0KDQojIyMjIyMjIyMjIyM=' (length=44)
9 => string 'IyMjIyMjIyMjIyMjIyMjIyMjIyMNCiMgV2Vic2VydmU=' (length=44)
10 => string 'cg0KIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM=' (length=44)
如有任何帮助,我们将不胜感激!
读取块后使用base64_decode
解码:
$data = '';
for ($i = 0; $i <= 500; $i++)
{
$result = $fm->getFileChunk('test.cfg', $pos, $len);
$chunk = $result->result;
if ($chunk != '')
{
$data .= base64_decode($chunk)
} else {
break;
}
$pos += $len;
}
file_put_contents("test.cfg", $data);
我不确定你所说的 "the data being written directly as text rather than being used to build the file" 是什么意思,但我猜你想要块数据,它是 base64 编码、解码并写入文件的。您可以使用 base64_decode()
函数来执行此操作。
php > echo base64_decode('I0FNUCBDb25maWd1cmF0aW9uIEZpbGUNCiMjIyMjIyM=');
#AMP Configuration File
#######
您需要为每个块执行此操作,因此您的 array_push()
应该是:
array_push(base64_decode($data), $chunk);
通过该更改,您的输出文件应该是正确的。
但是为什么要在写入文件之前构造一个数组,甚至是一个字符串呢?只需解码并写入检索到的每个块:
$outfile = fopen('test.cfg', 'w');
.
.
// in your loop..
fwrite($outfile, base64_decode($data));
.
.
fclose($outfile);
我目前正在使用一个 API,它提供了一个函数 "GetFileChunk",可以使用 pos 和 len 提取文件的一部分。没有文档,所以我正在试验并尝试先将文件数据拉入数组,然后将其写入文件。
到目前为止,我已经将数据读入数组,但我不确定如何将其写出。我试过 file_put_contents() 但这只是将行写为文本而不是从字节数据创建文件。
$fm = new FileManager();
$data = array();
$pos = 0;
$len = 32;
//while (($chunk = $fm->getFileChunk('test.cfg', $pos, $len)) != '')
for ($i = 0; $i <= 500; $i++)
{
$result = $fm->getFileChunk('test.cfg', $pos, $len);
$chunk = $result->result;
if ($chunk != '')
{
array_push($data, $chunk);
}
$pos += $len;
}
var_dump($data);
file_put_contents("test.cfg", $data);
这是我之后转储数组时的输出结果...
array (size=49)
0 => string 'I0FNUCBDb25maWd1cmF0aW9uIEZpbGUNCiMjIyMjIyM=' (length=44)
1 => string 'IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIw0KI0RPIE4=' (length=44)
2 => string 'T1QgRURJVCBXSElMRSBBTVAgSVMgUlVOTklORw0KIyM=' (length=44)
3 => string 'IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjDQo=' (length=44)
4 => string 'DQojIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM=' (length=44)
5 => string 'IyMNCiMgU2VjdXJpdHkNCiMjIyMjIyMjIyMjIyMjIyM=' (length=44)
6 => string 'IyMjIyMjIyMjIyMjIyMjIw0KU2VjdXJpdHkuRXhlY1A=' (length=44)
7 => string 'YXRoPUV4ZWMNClNlY3VyaXR5LkFsbG93Q29uY3VycmU=' (length=44)
8 => string 'bnRTZXNzaW9ucz1GYWxzZQ0KDQojIyMjIyMjIyMjIyM=' (length=44)
9 => string 'IyMjIyMjIyMjIyMjIyMjIyMjIyMNCiMgV2Vic2VydmU=' (length=44)
10 => string 'cg0KIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyM=' (length=44)
如有任何帮助,我们将不胜感激!
读取块后使用base64_decode
解码:
$data = '';
for ($i = 0; $i <= 500; $i++)
{
$result = $fm->getFileChunk('test.cfg', $pos, $len);
$chunk = $result->result;
if ($chunk != '')
{
$data .= base64_decode($chunk)
} else {
break;
}
$pos += $len;
}
file_put_contents("test.cfg", $data);
我不确定你所说的 "the data being written directly as text rather than being used to build the file" 是什么意思,但我猜你想要块数据,它是 base64 编码、解码并写入文件的。您可以使用 base64_decode()
函数来执行此操作。
php > echo base64_decode('I0FNUCBDb25maWd1cmF0aW9uIEZpbGUNCiMjIyMjIyM=');
#AMP Configuration File
#######
您需要为每个块执行此操作,因此您的 array_push()
应该是:
array_push(base64_decode($data), $chunk);
通过该更改,您的输出文件应该是正确的。
但是为什么要在写入文件之前构造一个数组,甚至是一个字符串呢?只需解码并写入检索到的每个块:
$outfile = fopen('test.cfg', 'w');
.
.
// in your loop..
fwrite($outfile, base64_decode($data));
.
.
fclose($outfile);