By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

fwrite for log

P: n/a

Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
Jan 7 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hanoi wrote:
Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
Hi,

Yes, you should prepare yourself for that indeed.
Use filelocking:
http://nl2.php.net/manual/en/function.flock.php

Filelocking is reliable, even on Windows.

You'll find a basic (but working) example on that page.

Regards,
Erwin Moller
Jan 7 '08 #2

P: n/a
On 7 Jan, 08:36, Hanoi <hin...@inwind.itwrote:
Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
Use an asynchronous logging mechanism (see the docs page for syslog())

C.
Jan 7 '08 #3

P: n/a
On Jan 7, 10:36 am, Hanoi <hin...@inwind.itwrote:
Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
If you use OS as gnu/linux, cpu will handle each process sequently.
Jan 8 '08 #4

P: n/a
On Jan 8, 9:03 am, Betikci Boris <pard...@gmail.comwrote:
On Jan 7, 10:36 am, Hanoi <hin...@inwind.itwrote:
Hi,
I would like to have a php file that log informations sent by an image
request.
For logging, I use fwrite to write on a file.
If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?
Thanks

If you use OS as gnu/linux, cpu will handle each process sequently.
No it won't. At the level of PHP code they will effectively be running
concurrently, not sequentially. You can have multiple processes
writing to a file, but in the absence of a mutex there is no guarantee
that one write will not occurr in the middle of a seperate write -
large writes are not atomic.

C.
Jan 8 '08 #5

P: n/a
If you use OS as gnu/linux, cpu will handle each process sequently.

Yes, I use a debian OS ...

So I can simply use fwrite without flock or any other asynchronous
logging mechanism?

Thanks


Jan 8 '08 #6

P: n/a
Betikci Boris wrote:
On Jan 7, 10:36 am, Hanoi <hin...@inwind.itwrote:
>Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks

If you use OS as gnu/linux, cpu will handle each process sequently.
Incorrect. Processes are run concurrently, not sequentially. Any
interrupt (i.e. disk i/o, timer, network traffic) will cause the OS to
switch back to the system to handle the interrupt. When processing
resumes, it will be the next process in the list. That is not
necessarily the same process which was executing before the interrupt.

That's why things like mutex semaphores and file locks exist.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 8 '08 #7

P: n/a
Hanoi wrote:
>If you use OS as gnu/linux, cpu will handle each process sequently.


Yes, I use a debian OS ...

So I can simply use fwrite without flock or any other asynchronous
logging mechanism?

Thanks


Not safely. If it were safe, there would be no need for flock().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 8 '08 #8

P: n/a

Ok. it's clear.

Thank you very much for your answers.
Jan 10 '08 #9

P: n/a
Greetings, Hanoi.
In reply to Your message dated Monday, January 7, 2008, 11:36:10,
I would like to have a php file that log informations sent by an image
request.
For logging, I use fwrite to write on a file.
For the logging purposes (considering there is not much of data sent to log)
I'm using this filter function:

function ob_debugfilter($str)
{
$str = date('Y-m-d H:i:s') . " {$str}\n";

if(defined('toolDebugFilterTarget') && toolDebugFilterTarget)
{
error_log($str, 3, toolDebugFilterTarget);
$str = NULL;
}

return $str;
}

To initialize it, You should define a constant toolDebugFilterTarget with the
path to Your logfile and start output buffering in Your code.
Don't forget to use ob_flush(); at each point of log output, itherwise all
Your log output from single run will be written in a single line.

I.e.

var_export($rc); @ob_flush();
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jan 15 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.