PHP script file loads indefinitely on browser when flock (LOCK_SH/LOCK_EX)












0















I came across this link while trying to learn how to lock files to prevent a script reading from a file as another is writing, or two scripts writing to the same file simultaneously.



I created two scripts, readandwritelock.php and readlock.php, the first script to retrieve the file with file_get_contents, append it and then write back to the same file with file_put_contents($file, $data, LOCK_EX), and the second that just retrieves the file with file_get_contents after flock($file, LOCK_SH).



<?php
//readandwritelock.php

$myfile = fopen('15-11-2018.txt', 'r+');
if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');

/*I commented this on my second test to see if file_put_contents will work.
After uncommenting and third test, it does not work anymore.

if (flock($myfile, LOCK_UN)) {

echo "Unlocked<br>";
}*/

$current .= "appending";

if (file_put_contents('15-11-2018.txt', $current, LOCK_EX)) {
echo "Success";
}
else {
echo "Failed";
//browser loads indefinitely so this does not run
}

fclose($myfile);
}
?>


The problem I am facing is that the first try I was able to file_get_contents after getting the lock, and then releasing the lock and proceed to append and file_put_contents($file, $data, LOCK_EX). However on the second try I decided to comment the releasing of the LOCK_SH lock to test and see what would happen. The script file loads indefinitely (Waiting for localhost...) on my browser, so I reverted back the changes for my third try, but this time the script file still loads indefinitely. It's as if the LOCK_SH was never released.



I must be doing something wrong, but I do not know what exactly it is. Could someone explain?



This was tested on XAMPP and macOS High Sierra and Chrome.



<?php
//readlock.php
//works as normal
$myfile = fopen('15-11-2018.txt', 'r');

if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');
echo $current;

if (flock($myfile, LOCK_UN)) {
echo "<br>Unlocked";
}
fclose($myfile);
}
?>









share|improve this question

























  • Have you tried removing the LOCK_EX flag in the file_put_contents call? I would assume this would block because the file is already locked.

    – Fitzi
    Nov 15 '18 at 8:47
















0















I came across this link while trying to learn how to lock files to prevent a script reading from a file as another is writing, or two scripts writing to the same file simultaneously.



I created two scripts, readandwritelock.php and readlock.php, the first script to retrieve the file with file_get_contents, append it and then write back to the same file with file_put_contents($file, $data, LOCK_EX), and the second that just retrieves the file with file_get_contents after flock($file, LOCK_SH).



<?php
//readandwritelock.php

$myfile = fopen('15-11-2018.txt', 'r+');
if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');

/*I commented this on my second test to see if file_put_contents will work.
After uncommenting and third test, it does not work anymore.

if (flock($myfile, LOCK_UN)) {

echo "Unlocked<br>";
}*/

$current .= "appending";

if (file_put_contents('15-11-2018.txt', $current, LOCK_EX)) {
echo "Success";
}
else {
echo "Failed";
//browser loads indefinitely so this does not run
}

fclose($myfile);
}
?>


The problem I am facing is that the first try I was able to file_get_contents after getting the lock, and then releasing the lock and proceed to append and file_put_contents($file, $data, LOCK_EX). However on the second try I decided to comment the releasing of the LOCK_SH lock to test and see what would happen. The script file loads indefinitely (Waiting for localhost...) on my browser, so I reverted back the changes for my third try, but this time the script file still loads indefinitely. It's as if the LOCK_SH was never released.



I must be doing something wrong, but I do not know what exactly it is. Could someone explain?



This was tested on XAMPP and macOS High Sierra and Chrome.



<?php
//readlock.php
//works as normal
$myfile = fopen('15-11-2018.txt', 'r');

if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');
echo $current;

if (flock($myfile, LOCK_UN)) {
echo "<br>Unlocked";
}
fclose($myfile);
}
?>









share|improve this question

























  • Have you tried removing the LOCK_EX flag in the file_put_contents call? I would assume this would block because the file is already locked.

    – Fitzi
    Nov 15 '18 at 8:47














0












0








0


0






I came across this link while trying to learn how to lock files to prevent a script reading from a file as another is writing, or two scripts writing to the same file simultaneously.



I created two scripts, readandwritelock.php and readlock.php, the first script to retrieve the file with file_get_contents, append it and then write back to the same file with file_put_contents($file, $data, LOCK_EX), and the second that just retrieves the file with file_get_contents after flock($file, LOCK_SH).



<?php
//readandwritelock.php

$myfile = fopen('15-11-2018.txt', 'r+');
if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');

/*I commented this on my second test to see if file_put_contents will work.
After uncommenting and third test, it does not work anymore.

if (flock($myfile, LOCK_UN)) {

echo "Unlocked<br>";
}*/

$current .= "appending";

if (file_put_contents('15-11-2018.txt', $current, LOCK_EX)) {
echo "Success";
}
else {
echo "Failed";
//browser loads indefinitely so this does not run
}

fclose($myfile);
}
?>


The problem I am facing is that the first try I was able to file_get_contents after getting the lock, and then releasing the lock and proceed to append and file_put_contents($file, $data, LOCK_EX). However on the second try I decided to comment the releasing of the LOCK_SH lock to test and see what would happen. The script file loads indefinitely (Waiting for localhost...) on my browser, so I reverted back the changes for my third try, but this time the script file still loads indefinitely. It's as if the LOCK_SH was never released.



I must be doing something wrong, but I do not know what exactly it is. Could someone explain?



This was tested on XAMPP and macOS High Sierra and Chrome.



<?php
//readlock.php
//works as normal
$myfile = fopen('15-11-2018.txt', 'r');

if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');
echo $current;

if (flock($myfile, LOCK_UN)) {
echo "<br>Unlocked";
}
fclose($myfile);
}
?>









share|improve this question
















I came across this link while trying to learn how to lock files to prevent a script reading from a file as another is writing, or two scripts writing to the same file simultaneously.



I created two scripts, readandwritelock.php and readlock.php, the first script to retrieve the file with file_get_contents, append it and then write back to the same file with file_put_contents($file, $data, LOCK_EX), and the second that just retrieves the file with file_get_contents after flock($file, LOCK_SH).



<?php
//readandwritelock.php

$myfile = fopen('15-11-2018.txt', 'r+');
if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');

/*I commented this on my second test to see if file_put_contents will work.
After uncommenting and third test, it does not work anymore.

if (flock($myfile, LOCK_UN)) {

echo "Unlocked<br>";
}*/

$current .= "appending";

if (file_put_contents('15-11-2018.txt', $current, LOCK_EX)) {
echo "Success";
}
else {
echo "Failed";
//browser loads indefinitely so this does not run
}

fclose($myfile);
}
?>


The problem I am facing is that the first try I was able to file_get_contents after getting the lock, and then releasing the lock and proceed to append and file_put_contents($file, $data, LOCK_EX). However on the second try I decided to comment the releasing of the LOCK_SH lock to test and see what would happen. The script file loads indefinitely (Waiting for localhost...) on my browser, so I reverted back the changes for my third try, but this time the script file still loads indefinitely. It's as if the LOCK_SH was never released.



I must be doing something wrong, but I do not know what exactly it is. Could someone explain?



This was tested on XAMPP and macOS High Sierra and Chrome.



<?php
//readlock.php
//works as normal
$myfile = fopen('15-11-2018.txt', 'r');

if (flock($myfile, LOCK_SH)) {

echo "Gotten lock<br>";
$current = file_get_contents('15-11-2018.txt');
echo $current;

if (flock($myfile, LOCK_UN)) {
echo "<br>Unlocked";
}
fclose($myfile);
}
?>






php fopen file-get-contents file-put-contents






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 3:46







iamhx

















asked Nov 15 '18 at 3:40









iamhxiamhx

16711




16711













  • Have you tried removing the LOCK_EX flag in the file_put_contents call? I would assume this would block because the file is already locked.

    – Fitzi
    Nov 15 '18 at 8:47



















  • Have you tried removing the LOCK_EX flag in the file_put_contents call? I would assume this would block because the file is already locked.

    – Fitzi
    Nov 15 '18 at 8:47

















Have you tried removing the LOCK_EX flag in the file_put_contents call? I would assume this would block because the file is already locked.

– Fitzi
Nov 15 '18 at 8:47





Have you tried removing the LOCK_EX flag in the file_put_contents call? I would assume this would block because the file is already locked.

– Fitzi
Nov 15 '18 at 8:47












1 Answer
1






active

oldest

votes


















0














The reason why your browser seems to load indefinitely is because your PHP file never finishes.



First you get a LOCK_SH (a shared or read lock) for your file, which is fine while you are reading the content.
The problem is that you also try to get a LOCK_EX (an exclusive lock) on the same file in the file_put_contents function. Therefore the file_put_contents functions blocks until all other locks (shared AND exclusive ones) are unlocked, which can't work (this is a deadlock).



For your code to work properly, you can either try to get an exlusive lock in the first place



if( flock($myfile, LOCK_EX) ) {
// ...


or you unlock the shared lock before you write



flock($myfile, LOCK_UN);
if ( file_put_contents('15-11-2018.txt', $current, LOCK_EX) ) {
// ...


In general it is a good idea to keep a locks life as short as possible. If you plan to make extensive manipulations to your data between reading and writing, I would recommend to unlock the file right after reading and lock it again right for writing.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312091%2fphp-script-file-loads-indefinitely-on-browser-when-flock-lock-sh-lock-ex%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The reason why your browser seems to load indefinitely is because your PHP file never finishes.



    First you get a LOCK_SH (a shared or read lock) for your file, which is fine while you are reading the content.
    The problem is that you also try to get a LOCK_EX (an exclusive lock) on the same file in the file_put_contents function. Therefore the file_put_contents functions blocks until all other locks (shared AND exclusive ones) are unlocked, which can't work (this is a deadlock).



    For your code to work properly, you can either try to get an exlusive lock in the first place



    if( flock($myfile, LOCK_EX) ) {
    // ...


    or you unlock the shared lock before you write



    flock($myfile, LOCK_UN);
    if ( file_put_contents('15-11-2018.txt', $current, LOCK_EX) ) {
    // ...


    In general it is a good idea to keep a locks life as short as possible. If you plan to make extensive manipulations to your data between reading and writing, I would recommend to unlock the file right after reading and lock it again right for writing.






    share|improve this answer




























      0














      The reason why your browser seems to load indefinitely is because your PHP file never finishes.



      First you get a LOCK_SH (a shared or read lock) for your file, which is fine while you are reading the content.
      The problem is that you also try to get a LOCK_EX (an exclusive lock) on the same file in the file_put_contents function. Therefore the file_put_contents functions blocks until all other locks (shared AND exclusive ones) are unlocked, which can't work (this is a deadlock).



      For your code to work properly, you can either try to get an exlusive lock in the first place



      if( flock($myfile, LOCK_EX) ) {
      // ...


      or you unlock the shared lock before you write



      flock($myfile, LOCK_UN);
      if ( file_put_contents('15-11-2018.txt', $current, LOCK_EX) ) {
      // ...


      In general it is a good idea to keep a locks life as short as possible. If you plan to make extensive manipulations to your data between reading and writing, I would recommend to unlock the file right after reading and lock it again right for writing.






      share|improve this answer


























        0












        0








        0







        The reason why your browser seems to load indefinitely is because your PHP file never finishes.



        First you get a LOCK_SH (a shared or read lock) for your file, which is fine while you are reading the content.
        The problem is that you also try to get a LOCK_EX (an exclusive lock) on the same file in the file_put_contents function. Therefore the file_put_contents functions blocks until all other locks (shared AND exclusive ones) are unlocked, which can't work (this is a deadlock).



        For your code to work properly, you can either try to get an exlusive lock in the first place



        if( flock($myfile, LOCK_EX) ) {
        // ...


        or you unlock the shared lock before you write



        flock($myfile, LOCK_UN);
        if ( file_put_contents('15-11-2018.txt', $current, LOCK_EX) ) {
        // ...


        In general it is a good idea to keep a locks life as short as possible. If you plan to make extensive manipulations to your data between reading and writing, I would recommend to unlock the file right after reading and lock it again right for writing.






        share|improve this answer













        The reason why your browser seems to load indefinitely is because your PHP file never finishes.



        First you get a LOCK_SH (a shared or read lock) for your file, which is fine while you are reading the content.
        The problem is that you also try to get a LOCK_EX (an exclusive lock) on the same file in the file_put_contents function. Therefore the file_put_contents functions blocks until all other locks (shared AND exclusive ones) are unlocked, which can't work (this is a deadlock).



        For your code to work properly, you can either try to get an exlusive lock in the first place



        if( flock($myfile, LOCK_EX) ) {
        // ...


        or you unlock the shared lock before you write



        flock($myfile, LOCK_UN);
        if ( file_put_contents('15-11-2018.txt', $current, LOCK_EX) ) {
        // ...


        In general it is a good idea to keep a locks life as short as possible. If you plan to make extensive manipulations to your data between reading and writing, I would recommend to unlock the file right after reading and lock it again right for writing.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 18:16









        FitziFitzi

        35519




        35519






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312091%2fphp-script-file-loads-indefinitely-on-browser-when-flock-lock-sh-lock-ex%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()