• Resolved Matta Cib

    (@matta-cib)


    Hello,
    First of all, thank you for the plugin WPCode Lite!
    I am asking for help with the following problem. The PHP code below does not work:
    $fp = fopen(“current/yesterday.txt”, “r+”); //contains numeric value like 123456789123
    $yesterday = fgets($fp, 9);
    echo “$yesterday”;
    I get the following error message (which I don’t know what to do with):
    “Snippet not activated, the following error was encountered: fgets(): Argument #1 ($stream) must be of type resource, bool given
    I have already used this code in a previously created snippet and this snippet still works fine. However, if I try to create a new snippet with the same code, I get the above error message.
    Can it be because I have meanwhile increased the PHP version to 8.0.28? And if so, what to do? (I’m using WP 6.1.1)
    Thanks and best regards
    Matta

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi Matta,

    It sounds like the error you are getting is that the fopen command can’t open the file you are pointing it to. From the documentation, fopen will return false if that pointer can’t be opened.

    I think you may need to use full path to the .txt file you are targeting.

    Thread Starter Matta Cib

    (@matta-cib)

    Hello Mircea,

    Thanks for your answer and the tip. But even with the complete, absolute path it doesn’t work…
    An “old” snippet, which is existing for a long time, works flawlessly online. And that is strange. This actually speaks against the possibility that the problem occurs due to a changed PHP version, because then an “old” snippet that contains this code should no longer work.
    I therefore rather suspect that the problem is due to a change within WPCode Lite, because there the activation of the snippet with said error message is prevented – the execution of the code in a previously saved and uploaded snippet works.
    Thanks and best regards
    matta

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi Matta,

    WPCode simply catches the error thrown by PHP and it prevents the activation of the snippet + forwards the message to you so you can see what prevented the snippet from being activated.

    If the snippet would get activated your site would throw the same fatal error. You can try adding a check before the fgets call to prevent the error in case you suspect that the issue only happens during activation, it would e good to have a check in any case to prevent the error and instead display a message or something similar when the file cannot be accessed.

    Another thing to consider is if your file has the right permissions to be accessed by the PHP script you are using.

    Thread Starter Matta Cib

    (@matta-cib)

    Hello Mircea,
    Thank you very much for your answer! For my part, I’m only replying today, because (to be honest) I didn’t understand your answer and hired a PHP specialist, and he has now found the error: Your hint about the absolute path was already correct, but I had entered the absolute path of the URL, but it must correctly be the absolute path of the root directory where the corresponding file is located. But now the problem is solved ??
    Best regards
    Matta

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘fgets()-error’ is closed to new replies.