我有这样的脚本写一个文件,但我有问题与损坏的文件,也许有人可以帮助我?
function savetofile () {
$dccgettemp = '';
$dccgettemp = fgets($GLOBALS['dcc_stream'], 512);
if ($dccgettemp != '') {
$GLOBALS['dccget'] = $dccgettemp;
$GLOBALS['currfilesize'] += strlen($dccgettemp);
fwrite($GLOBALS['handle'], $dccgettemp);
}
}
发布于 2020-01-20 04:34:34
似乎只从流中读取最多511个字节的数据,并将数据复制到输出流。
因此,当您需要写入所有文件内容时,必须重复该过程,直到不再读取数据为止。否则,您可以尝试在需要时将rèad缓冲区大小增加到适当的大小。
/* usage:
1. When used as class method:
$GLOBALS['currfilesize'] = $this->savetofile($GLOBALS['dcc_stream'], $GLOBALS['handle'], $GLOBALS['currfilesize']);
Or
2. When used as regular function:
$currfilesize = savetofile($dcc_stream, $handle, $currfilesize);
*/
public function savetofile ($in, $out, $initSize) {
if ($in && $out) {
// init output size, set to zero when null
$size = $initSize ?: 0;
// reset input file pointer (when needed)
//rewind($in);
// gets up to 1024 bytes data from stream
// iterate all file contents (when needed)
//while ($dccgettemp = fgets($in, 1025)) {
if ($dccgettemp = fgets($in, 1025)) {
// increment output size by buffer size
$size += strlen($dccgettemp);
// write to output stream
fwrite($out, $dccgettemp);
// forces a write of all buffered output
// to the resource pointed to by the file handle.
fflush($out);
}
return $size;
}
return $initSize;
}
您也可以使用stream_copy_to_stream()函数进行替代。
希望这能有所帮助。
https://stackoverflow.com/questions/59811229
复制相似问题