473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

PHP instances writing to the same file?

I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
PHP scripts running in the same web server process from accessing a
file at the same time".
In my case a single PHP script appends text strings to an existing text
file. Several clients may trigger the same script on the same web page
and thus more than one PHP instance might try to open and append text
to the same file at the same time. I would assume that using flock()
_does_ in fact prevent simultaneous access in this case. Am I correct?
Thanks!

Andrew

May 4 '06 #1
4 3099
ab****@yahoo.com writes:
I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
PHP scripts running in the same web server process from accessing a
file at the same time".
In my case a single PHP script appends text strings to an existing text
file. Several clients may trigger the same script on the same web page
and thus more than one PHP instance might try to open and append text
to the same file at the same time. I would assume that using flock()
_does_ in fact prevent simultaneous access in this case. Am I correct?


You are correct

There are different types of locks though but LOCK_EX should de what
you want..

--
Henrik Hansen
May 4 '06 #2
ab****@yahoo.com wrote:
I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
PHP scripts running in the same web server process from accessing a
file at the same time".
In my case a single PHP script appends text strings to an existing text
file. Several clients may trigger the same script on the same web page
and thus more than one PHP instance might try to open and append text
to the same file at the same time. I would assume that using flock()
_does_ in fact prevent simultaneous access in this case. Am I correct?
Thanks!

Andrew


What is meant I think is that flock() does not work on multithreaded
environments like Windows, where all multiple PHP instances run inside
the same process.

May 4 '06 #3
What OS is the server that this script is running on? Flock will work
to lock a file if you are running your script on a server with an OS
that is compatible with flock. Both NFS and FAT file systems are
incompatible.

Here is a flock sample from (http://us3.php.net/flock):

$fp = fopen("/tmp/lock.txt", "w+");
if (flock($fp, LOCK_EX)) { // do an exclusive lock
fwrite($fp, "Write something here\n");
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't lock the file !";
}
fclose($fp);
Regards

Jon Tjemsland
ab****@yahoo.com wrote:
I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
PHP scripts running in the same web server process from accessing a
file at the same time".
In my case a single PHP script appends text strings to an existing text
file. Several clients may trigger the same script on the same web page
and thus more than one PHP instance might try to open and append text
to the same file at the same time. I would assume that using flock()
_does_ in fact prevent simultaneous access in this case. Am I correct?
Thanks!

Andrew


May 4 '06 #4
Thanks. It's a server running on Linux.

Andrew
jo***********@gmail.com ha scritto:
What OS is the server that this script is running on? Flock will work
to lock a file if you are running your script on a server with an OS
that is compatible with flock. Both NFS and FAT file systems are
incompatible.

Here is a flock sample from (http://us3.php.net/flock):

$fp = fopen("/tmp/lock.txt", "w+");
if (flock($fp, LOCK_EX)) { // do an exclusive lock
fwrite($fp, "Write something here\n");
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't lock the file !";
}
fclose($fp);
Regards

Jon Tjemsland
ab****@yahoo.com wrote:
I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
PHP scripts running in the same web server process from accessing a
file at the same time".
In my case a single PHP script appends text strings to an existing text
file. Several clients may trigger the same script on the same web page
and thus more than one PHP instance might try to open and append text
to the same file at the same time. I would assume that using flock()
_does_ in fact prevent simultaneous access in this case. Am I correct?
Thanks!

Andrew


May 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: L Robbins | last post by:
I am a newcomer to PHP and am writing a script which dynamically creates instances of a class with names like $object1, $object2, etc.. I now want my script to be able to list all of the objects...
4
by: Kay Schluehr | last post by:
Hi people, I wonder why the isinstance() function is sensitive about the import path i.e. the result depends not only on the class and the instance but also on how a class is imported? ...
1
by: Bruce | last post by:
I am writing an installer that needs to put a dll file with a bunch of extended stored procedures in the BINN directory. The problem is that if the machine is running multiple instances of SQL...
1
by: Vlad | last post by:
Is there any way to install multiple instances of the same windows service designed with VS.NET 2003? I tried copying the binaries into a separate folder and then copying registry entries for the...
6
by: Chris Tyson | last post by:
I created an Access 97 database a while ago. It is protected by Workgroup Security, and it is not split. In order to compare different sets of results, a colleague has suggested that it would be...
4
by: Roland Riess | last post by:
Hi all, at the moment i am developing an app which is sort of an interface to copy data from one database to another and it shall run as a service. As there are several databases the app must be...
0
by: Robinson | last post by:
At present I'm generating a log file during program execution by creating a console log trace and sending it to an external file thus: Debug.Listeners.Add(New...
5
by: kkirtac | last post by:
Hi, i have a class "myClass", and i want to return two instances of the same class from a function. I define an array as follows : "myClass sample ;" and i initialize two instances: "myClass...
0
by: jigsmshah | last post by:
I have a windows service developed in C#.I am reading the connection string from an ini file and also i am reading 3 image file from the bin directory. now the new requirement is that there will be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.