Forum Moderators: coopster

Message Too Old, No Replies

data handling w/out a dbase

fopen fread fwrite xml

         

clarkepeters

6:03 pm on Mar 22, 2008 (gmt 0)

10+ Year Member



I have an xml file that stands as the base to my bulletin board application. A single instance (copy) of this file could be accessed by several users at a time, who will delete, edit, or add records as they wish. The several users will then submit their separate updated data which is merged back into the single file (one submission at a time).

I've built the parser for the merging but cannot figure out how to handle the data's actual file updating (saving).
Currently, I first fopen the file so that I can then flock it. I then get and manipualate the data and fwrite and fclose.

the problem is, if I use fopen with "w" and somebody aborts the script, then all the data is lost and the whole application fails. If I use fopen with "r+" that would be just perfect except that when I write, since it doesn't delete the whole file like "w", I sometimes have junk at the end of my file, which again, in keeping with the exactness of xml, will cause my script to fail. (the file size can grow and shrink) (append is not an option).

I thought about opening it with fopen "r" and then after data manipulation do fclose, then immediately fopen with "w" and flock and then fwrite and fclose. But what if I have users stacked up waiting to access the file? When I do the first fclose (for the "r" option) will I get blocked out by others who have been waiting for the file so that my script might not get to the file before someone else grabs an un-updated version of it? Once the update process starts, I cannot allow the chance of another getting the un-updated version.

My data's not sensitive so I don't need a database (and I want installation to be easy for my clients). So I'm trying to handle this myself. Any help would be appreciated.

eelixduppy

5:04 am on Mar 23, 2008 (gmt 0)



Welcome to WebmasterWorld! :)

Are you running php5? If so, SimpleXML [php.net] will help you manipulate your XML files much easier than the standard file-writing methods If I were you I would try to implement a solution using that instead. Give it a try and see what you get out of it. If you do not have php5 then I suggest you upgrade ;)

clarkepeters

7:53 am on Mar 23, 2008 (gmt 0)

10+ Year Member



Hope this isn't a double post, I seem to have lost my first try.

I am familiar with SimpleXML, DOM, Xpath etc. but do not prefer it. I have already written my own parsing procedures. My concern is not with the parsing but with user access to the actual xml file. As I mentioned above, there are two ways I can fopen, one with "w" and one with "r+", neither of which satisfies my needs. "w" will delete all information inside the file if someone aborts, and "r+" will leave extra data at the end of the file after a new save--both situations will kill my application.

I need a better way, or need to know that if I fopen for reading ("r") and then fclose, and then fopen again for writing ("w"), will a user be able to slip in between the fclose and fopen and grab the xml file before I have saved the updates to the file, because I can't allow that to happen.

thanks for the welcome and the heads up on SimpleXML :)