K
K
KOS_MOS2011-07-13 13:10:18
PHP
KOS_MOS, 2011-07-13 13:10:18

Lazy creation of files in PHP (Python)?

There is a system that keeps logs using Zend_Log_Writer_Stream, messages are written to files.
When creating a Zend_Log_Writer_Stream, you need to pass a link to the file resource, that is, the file needs to be created - but not the fact that the system will issue messages that will be logged.
Therefore, there is a bunch of empty files.
Is there a mechanism for delayed file creation in PHP (for Python is also relevant) - that is, the file will be created only when writing to it begins and will not be created if there is no writing to it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zizop, 2011-07-13
@zizop

Let's talk about PHP. As far as I know, fopen opens a handle and creates a file accordingly. And if you do not call fopen, but implement it at a higher level, i.e. intercept the write operation and check if the file is open. If not open, then open. In general, I would inherit from Zend_Log_writer_Stream and add what I need.

A
Anatoly, 2011-07-13
@taliban

In php / python there is a wonderful thing called OOP, with its help you can create an object, inherit from (in this case) Zend_Log_Writer_Stream and, oddly enough, override the necessary methods, replacing it with the expected logic.

S
SolarSoul, 2011-07-13
@SolarSoul

It seems to me that it is possible to write a simple wrapper over the file class, in Python I think it is definitely possible.
At worst, you can simply check the size of the file in the wrapper's destructor, and delete the file if it's empty.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question