Blank space after fwrite

Hi Everyone,

I have not used php file operations very much and am coming across a curious issue that after I do an fwrite I get a blank space at the very top of the log file I am writing.

class FileErrorLogger extends  ErrorHandler{
    private $fh;
        
    public function __construct($file_handle){
        $this->fh = $file_handle;
    }
    
    public function write($msg) {
        fwrite($this->fh, "\
".date('Y-m-d:i:s: ').$msg);
    }
    
    public function update($error) {
        //$error = $error_handler->getError();
        $this->write($error);
    }
}

This is how I create the file (I unlink it as this is a method of a SimpleTest class:

function setup() {
            if($this->test_file) {
                unlink($this->test_file);
            }
            $this->test_file  =  "var/www/domain/lib/common/logs/test_file.log";
            //echo '<br />' . $this->test_file . '<br />';
            $this->fh = fopen($this->test_file, 'w');
            
            chmod("var/www/domain/lib/common/logs/test_file.log", 0600);
            if (!is_writable  ( $this->test_file )) {
                echo 'file or folder not writeable.<br />' ;
                exit;
            }
        }

Here is the method in action:

function TestWriteIsTimeStamped() {
            $content = 'test'.rand(10,100);
            $log =& new FileErrorLogger($this->fh);
            $log->write($content);
            ...
        }

And this is the contents of the file (the
\r shows as a blank space in my text file, I have used them to illustrate that there is a line and a return before the content is written:

\
\
2011-02-14:31:04: test13

Can you tell me how to remove the top space from the file?

Regards,
Steve

It looks like the following is to blame:


fwrite($this->fh, "\
".date('Y-m-d:i:s: ').$msg);
//                 ^^ 

That \ means that a newline character is prepended every time you call the FileErrorLogger::write() method.

Hi Salathe

Thanks I missed the obvious :blush:

Regards,
Steve