Search code examples
phpfile-put-contents

file_put_contents doesn't give write permissions error


I've been using file_put_contents in order to create txt files inside a specified folder that has write permissions:

file_put_contents($dir.$file.'.txt', $content);

Editing my code, I made a mistake: I wrote $dir = '/../../xxx/yyy/'; (that actually doesn't exist) instead of $dir = '../xxx/yyy/'; (right directory).

Obviously, no file has been created (all other folders are read-only), but I didn't get any error message about it.

Why?

P.S.: I get other error messages on the same PHP page, but not the above one.


Solution

  • From the docs:

    "This function returns the number of bytes that were written to the file, or FALSE on failure"

    https://www.php.net/manual/en/function.file-put-contents.php

    I.e use something like $result = file_put_contents($dir.$file.'.txt', $content);

    And check if it's true or not