Jump to content

How To Write An Object To A File In Php ?


omerjerk

Recommended Posts

function object2file($dir, $object) {
 file_put_contents(rtrim($dir, '\/') . DIRECTORY_SEPARATOR . get_class($object) . '.obj', serialize($object));
}

 

OK ... thanx... It worked fine now ...

 

And the second question is that can't it append the data .,... It is writing the whole file again deleting the previous fie ..

Have you tried anything so far? Like read the manual page file_put_contents. There is an FILE_APPEND constant that you can pass...

 

function object2file($dir, $object, $mode = 0) {
  file_put_contents(rtrim($dir, '\/') . DIRECTORY_SEPARATOR . get_class($object) . '.obj', serialize($object), $mode);
}

 

object2file('/path/to/dir', new stdClass, FILE_APPEND);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.