Page 1 of 1

restricting file access

Posted: Fri Apr 01, 2005 5:27 pm
by brandan
i have a script that opens a flat file for reading, then places information read from the flat file into a database. i'm anticipating that more than one person will view the page at any given moment, and that possibly this script will be run at or around the same time, resulting in the same file being opened and duplicate entries being placed into the database.

does anybody know of any way to approach this, or if it's not even an issue? perhaps php closes off access to files it's currently working with. i'm not sure.

Posted: Fri Apr 01, 2005 5:33 pm
by feyd
  1. set a flag in the database
  2. lock the file for exclusive access.
those are about the only things I know of. Making the file require a username and password to even open can help. As well as putting it in a "safe" place.

If you restricted the calling of the script to say, user x, y, and z.. that'd also limit the chances.. but locking the data file and setting a flag in the database are probably the most "secure" ways of locking.

Posted: Fri Apr 01, 2005 5:42 pm
by brandan
well here's an example of what i'm doing, because i might not have been very clear in my original post. i have the music i've recently listened to logged into a flat file using a plugin for foobar2000, then parsed by another script, and placed into a database to be read by other scripts to be presented on a website. the flat file is read after the index.php is loaded to sort of 'automate' the parsing of the flat file, so i won't have to do it manually. if that's not clear, i'll post some of the script.

Posted: Fri Apr 01, 2005 5:44 pm
by feyd
my reply works with that.

Posted: Fri Apr 01, 2005 5:45 pm
by brandan
alright, i'll look some of that up.

Posted: Fri Apr 01, 2005 6:21 pm
by brandan
oh, and afterwards the file is removed using unlink().

i think flock() is what i'm looking for. i'll mess with it later.