Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1477 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP - Reading a file but not locking it

Thread title: PHP - Reading a file but not locking it
Reply  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
07-17-2010, 06:55 AM
#1
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old  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

Reply With Quote
07-17-2010, 02:56 PM
#2
cableguy is offline cableguy
Status: Member
Join date: Mar 2010
Location:
Expertise:
Software:
 
Posts: 156
iTrader: -1 / 0%
 

cableguy is an unknown quantity at this point

  Old

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?

Reply With Quote
07-18-2010, 10:34 AM
#3
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

How would sqlLite help?

Reply With Quote
07-18-2010, 01:37 PM
#4
Immersion is offline Immersion
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 918
iTrader: 5 / 100%
 

Immersion is on a distinguished road

  Old

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.

Reply With Quote
07-18-2010, 01:46 PM
#5
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

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.

Reply With Quote
07-18-2010, 02:25 PM
#6
Immersion is offline Immersion
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 918
iTrader: 5 / 100%
 

Immersion is on a distinguished road

  Old

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..

Reply With Quote
07-18-2010, 05:07 PM
#7
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

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.

Reply With Quote
07-18-2010, 05:48 PM
#8
Immersion is offline Immersion
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 918
iTrader: 5 / 100%
 

Immersion is on a distinguished road

  Old

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.

Reply With Quote
Thanked by:
geforce (07-18-2010)
07-18-2010, 06:44 PM
#9
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

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!

Reply With Quote
07-18-2010, 06:54 PM
#10
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

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.

Reply With Quote
Reply  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed