2

Ok, ok .. so right now I'm freaking out.

index1.php

<?
function write_file($filepath,$filecontent) {
    $openedfile = fopen($filepath,"w+"); //replace with $openedfile = fopen($filepath,"a"); just in case
    flock($openedfile, LOCK_EX);
//add here fclose($openedfile); to work
//add here $openedfile = fopen($filepath,"w+"); to work
    fwrite($openedfile,$filecontent);
    flock($openedfile, LOCK_UN);
    fclose($openedfile);
}
function read_file($filepath) {
    $openedfile = fopen($filepath,"r+");
    flock($openedfile, LOCK_SH);
    sleep(10);
    $filecontent = file_get_contents($filepath);
    flock($openedfile, LOCK_UN);
    fclose($openedfile);
    return $filecontent;
}
write_file("Readme.txt","test 1");
$f1 = read_file("Readme.txt");
echo $f1;
?>

index2.php

<?
function write_file($filepath,$filecontent) {
    $openedfile = fopen($filepath,"w+"); //replace with $openedfile = fopen($filepath,"a"); to work
    flock($openedfile, LOCK_EX);
//add here fclose($openedfile); to work
//add here $openedfile = fopen($filepath,"w+"); to work
    fwrite($openedfile,$filecontent);
    flock($openedfile, LOCK_UN);
    fclose($openedfile);
}
function read_file($filepath) {
    $openedfile = fopen($filepath,"r+");
    flock($openedfile, LOCK_SH);
    $filecontent = file_get_contents($filepath);
    flock($openedfile, LOCK_UN);
    fclose($openedfile);
    return $filecontent;
}
write_file("Readme.txt","test 2");
$f1 = read_file("Readme.txt");
echo $f1;
?>

I run index1.php, then after 2 sec I run index2.php. Index2.php waits for index1.php as expected but index1.php shows nothing after 10 sec, while index2.php shows "test 2". What is going on?

EDIT: I figured it out :D. I changed

$openedfile = fopen($filepath,"w+");

to

$openedfile = fopen($filepath,"a");

in second php and it doesn't wipe readme.txt upon index2.php execution anymore.

1
  • Looks like you release the lock prior writing to the file. See also fflush. Commented Aug 4, 2012 at 9:46

1 Answer 1

1

Doesn't this has to do something with the fact your readme.txt is actually empty for a very short period of time when you open the file to write in it? I thought PHP took out the entire text, and than replaced it with the entire text + addings. When index1.php wants to read the file, index2.php has just cleared it perhaps? You can check this in the apache logs by the way.

EDIT: also, immediately after unlocking the file, index2.php takes control over it, overwriting TEST 1 with TEST 2.

Sign up to request clarification or add additional context in comments.

6 Comments

You're right. I checked again and readme.txt is empty after I run index2.php. I was miss-leaded because earlier when I checked readme.txt through FTP (server is not local) after index2.php was executed, readme.txt was containing "test 1". Does FTP have some cache or somthing?
FTP? With no word in your question you mentioned that this is related to FTP. And yes, FTP does things different than the local file-system because it is just a protocol and a server talking with it. It then depends on the FTP server and it's configuration what exactly happens. Check the configuration and documentation of your FTP server to find out how you can influence its behavior for your good.
Not that I'm aware of actually. But perhaps it's just easier/saver to put the content of readme.txt in a database? That's faster to edit and change.
@hakre It isn't related to FTP. He just checked the content of readme.txt to verify if it was changed or not.
@DjerroNeth: OP asked in a comment about FTP, that's why I wondered.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.