|
|
|
|
Thread title: PHP - Reading a file but not locking it |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
07-17-2010, 06:55 AM
|
#1
|
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
Posts: 216
|
PHP - Reading a file but not locking it
Hi,
Im hoping someone can help me with a small problem im having.
I have a program which is logging actions from a windows application. This application puts actions into a text file which im reading and formatting to put into a database. All that is working fine.
But! When reading the log file with PHP if the program wants to add an action to that log file it says it cant create the log file, obviously because im reading it with php at that time.
Is there anyway I can read it but also allow the application to write to it no matter what. I can open the log file in notepad and the application will strill write to it.
Thanks for any help.
Martin
|
|
07-17-2010, 02:56 PM
|
#2
|
Status: Member
Join date: Mar 2010
Location:
Expertise:
Software:
Posts: 156
|
it might not be doable in windows..
accroding to this >> http://php.net/manual/en/function.flock.php it can't be done on Windows.
"By default, this function will block until the requested lock is acquired; this may be controlled (on non-Windows platforms) with the LOCK_NB option documented below."
Why don't you use something like sqlite?
|
|
07-18-2010, 10:34 AM
|
#3
|
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
Posts: 216
|
|
|
07-18-2010, 01:37 PM
|
#4
|
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
Posts: 918
|
SQLite is a database which would allow concurrent access to the same data. SQLite is also file based and most languages can use it.
I don't know much about file locking on the file system though if you text file approach is possible.
|
|
07-18-2010, 01:46 PM
|
#5
|
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
Posts: 216
|
I would adopt that method but my application is basically third party, and is just piggy backing off the text file the program is making, I have no access to the code of that program.
So I am basically using the programs log file as a trigger.
|
|
07-18-2010, 02:25 PM
|
#6
|
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
Posts: 918
|
Would copying the file using php and then read the copy be better. That way the original file should only be locked for a lot less time i would imagine. Just an idea..
|
|
07-18-2010, 05:07 PM
|
#7
|
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
Posts: 216
|
I've tried that method also, and now and then it catches it out, which I cant afford to be missing logged actions.
Im beginning to think there is not a solution to this.
|
|
07-18-2010, 05:48 PM
|
#8
|
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
Posts: 918
|
Just did a search but there wasn't much. Just your digital point thread came up mainly that had the same question.
How about monitoring the file for changes, either by file size of modification timestamp. Then the last change quickly with php starting from the end of the file. That way the file will only be read when changed and just get the latest change quickly.
|
|
07-18-2010, 06:44 PM
|
#9
|
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
Posts: 216
|
Well ive also done this, checked filesize and compared it to a previous filesize to check for changes, but checking for filesize seems to "lock" it aswell.
The scripts needs to check the file every 3 seconds, because a certain trigger opens a popup after a certain action is made. So checking every 10 seconds would eliminate the problem but render the script useless.
If I open the log file in notepadd for editing, the program can still write to the file. Or am I right in thinking the file content will be loaded into mermory and the file is not in use until saving again.
Is it just PHP is locking it? Would a seperate windows app reading the file eliminate the problem?
Think im really stuck.
Thanks for your help so far though. Much appreciated!
|
|
07-18-2010, 06:54 PM
|
#10
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
I am not intimately familiar with how php reads the files, so I don't know a solution to your problem purely in PHP. This is one of the issues with high level languages; you don't have much control over anything specific.
Your presumption of notepad is correct, it only accesses the file when it needs to. Perhaps you would write a quick app in C to do your bidding with the file and call it via command line.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|