RE: [PHP] __sleep() strange behavior with file writting and SESSION using
| From: | Andrés Robinet | Date: | Tue, 20 Nov 2007 22:48:29 +0000 |
| Subject: | RE: [PHP] __sleep() strange behavior with file writting and SESSION using | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-264883@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Andrés Robinet [mailto:agrobinet@bestplace.biz]
> Sent: Tuesday, November 20, 2007 7:41 PM
> To: php-general@lists.php.net
> Subject: RE: [PHP] __sleep() strange behavior with file writting and
> SESSION using
>
> > -----Original Message-----
> > From: Julien Pauli [mailto:doctorrock83@gmail.com]
> > Sent: Tuesday, November 20, 2007 3:51 PM
> > To: php-general@lists.php.net
> > Subject: [PHP] __sleep() strange behavior with file writting and
> > SESSION using
> >
> > Consider that very simple code, that runs on PHP 5.2.5 onto a Windows
> > machine :
> > <?php
> > class a
> > {
> > public $b;
> >
> > public function __sleep()
> > {
> > file_put_contents("log.txt","ok" . PHP_EOL,FILE_APPEND);
> > echo "done!";
> > return array();
> > }
> > }
> >
> > $a = new a;
> > serialize($a);
> > ?>
> >
> > No problem here, log.txt is writtable, when it passes on the
> > serialize()
> > instruction, it goes to __sleep and works well.
> > "OK\r\n" is appended to the log file , and "done!" is displayed.
> >
> >
> >
> > Now consider this :
> >
> > <?php
> > session_start();
> > class a
> > {
> > public $b;
> >
> > public function __sleep()
> > {
> > file_put_contents("log.txt","ok" . PHP_EOL,FILE_APPEND);
> > echo "done!";
> > return array();
> > }
> > }
> >
> > $a = new a;
> > $_SESSION['obj'] = $a;
> > ?>
> >
> > In this case, when the object is going in the session, it naturally
> > passes
> > throught __sleep().
> > The problem is that file_put_contents() doesn't work -> the file is
> not
> > appended "OK\r\n" as it should be.
> > "done!" is displayed , and if you look at the return value of
> > file_put_contents ( number of bytes that have been written ) : it's
> all
> > right ! It simply doesn not write to the file.
> >
> > Anyone has an idea ?
> > Is this a bug ?
> > Thx :)
>
> Maybe I drank too much coffee last night, but this is working for me if
> I
> add dirname(__FILE__) to "log.txt" (PHP version 5.2.4) and not
> otherwise
>
> <?php
> session_start();
> class a
> {
> public $b;
>
> public function __sleep()
> {
> file_put_contents(dirname(__FILE__)."/log.txt","ok" .>
PHP_EOL,FILE_APPEND);
> echo "done!";
> return array();
> }
> }
>
> $a = new a;
> $_SESSION['obj'] = $a;
> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
Just wanted to add, I found a log.txt at "D:\xampp\apache" which happens to
be the root of the apache installation on my system... so, moral of the
story, the current dir is not always the script's dir.
dirname(__FILE__)."/log.txt" will do the trick.
Rob