I tried to use flock but it just ends up deleting the contents of my file. I want flock() to let other application read, but not write to this file. PHP code: $filename = '../text/file.txt'; $fw = fopen($filename, "wrb"); $fl = flock($fw, LOCK_SH); $data = 'hello'; $numbytes = fwrite($fw, $data); flock($fw, LOCK_UN); Code (markup): Thanks. And mabye someone could tell me how to change a PHP array into a Javascript array?
You are opening the file with "write and read" access....... write will open at position 0 and truncate the file, hence why its overwriting. If you need to append to the file then you should be looking to use a+
I'm confused. What do you mean by a+ I googled it and came up with nothing. Would this work to append my data? PHP code: $filename = '../text/file.txt'; $fw = fopen($filename, "wrb"); $fl = flock($fw, LOCK_SH); [COLOR="blue"]$data = fread($fw, filesize($filename)) $data .= ' hello';[/COLOR] $numbytes = fwrite($fw, $data); flock($fw, LOCK_UN); Code (markup):
When you are opening the file here; $fw = fopen($filename, "wrb"); PHP: You are opening with mode "wrb" - by opening a file for Write, it will truncate the file to 0 bytes; that is, remove all the data. You should open with mode a+ and not mode wr if you want to add onto the end of it. No - as the file will be truncated when opened (so it will be 0 bytes).