PHP的gzuncompress与文件读写错误 [英] PHP gzuncompress with file read and write errors

查看:176
本文介绍了PHP的gzuncompress与文件读写错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跟踪通过脚本发生的事件的函数。为了有效地使用我的资源,我决定压缩它生成的数据。然而,我不断收到此错误:

$ p $ 未知的错误类型:[2] gzuncompress()[function.gzuncompress]:数据错误

以下是函数:

pre > function eventlog($ type,$ message){
//类型:账户,运行,队列,系统
//设置文件名/位置
$ eventfile ='/myprivatedirectory/'.date('Ymd').$type.'.log';
$ b $ if(file_exists($ eventfile)){
while(!is_writable($ eventfile)){clearstatcache();}

$ fh_log = fopen($ eventfile, 'R +');
flock($ fh_log,LOCK_EX);
$ logcontents = gzuncompress(fread($ fh_log,filesize($ eventfile)));

倒带($ fh_log);
ftruncate($ fh_log,0);

$ logcompressed = gzcompress($ logcontents。$ message。\\\
);
fwrite($ fh_log,$ logcompressed);
flock($ fh_log,LOCK_UN);
fclose($ fh_log);
} else {
$ fh_log = fopen($ eventfile,'w');
flock($ fh_log,LOCK_EX);
$ logcompressed = gzcompress($ message。\\\
);
fwrite($ fh_log,$ logcompressed);
flock($ fh_log,LOCK_UN);
fclose($ fh_log);






$ b因此,每天午夜,一个新的错误日志是创建任何上述事件发生(帐户,运行,队列,系统),否则每个新的事件追加到尊重的日志文件。



我很想保持压缩,但我不能有这些错误,任何人都可以请帮忙?感谢提前。
解决方案

我认为执行是错误的我不会建议你gzcompress($ message。\ N);每一条消息...

我认为你应该做的就是在一天结束的时候你可以压缩整个日志文件,这样更有效率。



使用

  file_put_contents 




$ p $ eventfile ='/myprivatedirectory/'.date('Ymd').$type.'.log';
$ eventfileCompressed ='/myprivatedirectory/'.date('Ymd').$type.'.gz';
$ gz = gzopen($ eventfilecompressed,w9);
gzwrite($ gz,file_get_contents($ eventfile));
gzclose($ gz);

阅读文件

  $ zd = gzopen($ eventfilecompressed,r); 
$ zr = gzread($ zd,$ fileSize);
gzclose($ zd);

这种方法可以为您节省更多的处理能力

I have a function that keeps track of events that happen through out the script. In an effort to use my resources effectively, I decided to compress the data that it generates. However, I keep getting this error:

Unknown error type: [2] gzuncompress() [function.gzuncompress]: data error

Here's the function:

function eventlog($type, $message){
    // Types: account,run,queue,system
    // Set up file name/location
    $eventfile = '/myprivatedirectory/'.date('Ymd').$type.'.log';

    if(file_exists($eventfile)){
        while(!is_writable($eventfile)){clearstatcache();}

        $fh_log = fopen($eventfile,'r+');
        flock($fh_log, LOCK_EX);
        $logcontents = gzuncompress(fread($fh_log,filesize($eventfile)));

        rewind($fh_log);
        ftruncate($fh_log, 0);

        $logcompressed = gzcompress($logcontents.$message."\n");
        fwrite($fh_log,$logcompressed);
        flock($fh_log, LOCK_UN);
        fclose($fh_log);
    } else {
        $fh_log = fopen($eventfile,'w');
        flock($fh_log, LOCK_EX);
        $logcompressed = gzcompress($message."\n");
        fwrite($fh_log,$logcompressed);
        flock($fh_log, LOCK_UN);
        fclose($fh_log);
    }
}

So everyday, at midnight, a new error log is created as any of the above events occur (account,run,queue,system), otherwise each new event is appended to the respectful log file.

I would love to keep the compression, but I can not keep having these errors, can anyone please help? Thanks in advance.

解决方案

I think the implementation is all wrong i would not advice you to gzcompress($message."\n"); every message ...

I think what you should do is that at the end of the day you can compress the whole log file which is more efficient

Save your information using

  file_put_contents

At the end of the day

$eventfile = '/myprivatedirectory/'.date('Ymd').$type.'.log';
$eventfileCompressed = '/myprivatedirectory/'.date('Ymd').$type.'.gz';
$gz = gzopen($eventfileCompressed ,"w9");
gzwrite($gz, file_get_contents($eventfile));
gzclose($gz);

To read the file

$zd = gzopen($eventfileCompressed,"r");
$zr = gzread($zd,$fileSize);
gzclose($zd);

This approach would save you more processing power

这篇关于PHP的gzuncompress与文件读写错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆