首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >fwrite()文件大小损坏,我需要scripthelp

fwrite()文件大小损坏,我需要scripthelp
EN

Stack Overflow用户
提问于 2020-01-19 22:55:30
回答 1查看 52关注 0票数 0

我有这样的脚本写一个文件,但我有问题与损坏的文件,也许有人可以帮助我?

代码语言:javascript
运行
复制
function savetofile () {
    $dccgettemp = '';
    $dccgettemp = fgets($GLOBALS['dcc_stream'], 512);
    if ($dccgettemp != '') {
        $GLOBALS['dccget'] = $dccgettemp;
        $GLOBALS['currfilesize'] += strlen($dccgettemp);
        fwrite($GLOBALS['handle'], $dccgettemp);
    }
}
EN

Stack Overflow用户

发布于 2020-01-20 04:34:34

似乎只从流中读取最多511个字节的数据,并将数据复制到输出流。

因此,当您需要写入所有文件内容时,必须重复该过程,直到不再读取数据为止。否则,您可以尝试在需要时将rèad缓冲区大小增加到适当的大小。

代码语言:javascript
运行
复制
/* 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()函数进行替代。

希望这能有所帮助。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59811229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档