using one file by two functions at the same time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
urosh
Forum Newbie
Posts: 2
Joined: Wed Jan 03, 2007 6:18 pm

using one file by two functions at the same time

Post by urosh »

Hi all, i am new with PHP, and i would like to know if it is possible to use one file by two different functions at the same time. That is, one function will read information from the top of the file, while second function will add information on the bottom of the file. Second function is much faster in its job so ther is no problem that first function will come to the end of the file before second function finish its work.

Is this possible, or i need to break the file into more files that will be approached by only one function at the time?

Thanks in advance
Urosh
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

I dont think you can do that simultaniously, but you can use file() to get the file into an array (each line in each index of the array), then close the file, and then write to it while reading from the array.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're wishing them to run in parallel, that's not typically possible (unless you fork a process) or make the request parallel.
urosh
Forum Newbie
Posts: 2
Joined: Wed Jan 03, 2007 6:18 pm

Post by urosh »

Yes, idea is to fork these two processes. Problem is that i dont know if it is possible for these two programs to use same file at same time, one to write to file, one to read from the file.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering that ability is quite server dependent, I would suggest testing it if you need it.

The question is, do you really need it to work? If so, you may want to consider using a database.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Yes it is possible,
simple use flock function to lock file on reading or writing.
Post Reply