|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76132 Documentation lacks a warning about file_put_contents()
Submitted: 2018-03-22 00:11 UTC Modified: 2018-03-22 03:02 UTC
From: briethings at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.6.34 OS: any
Private report: No CVE-ID: None
 [2018-03-22 00:11 UTC] briethings at gmail dot com
From manual page:

Documentation doesn't mention that, if the directory specified in $filename doesn't exist, file_put_contents() *silently* fails, with not even a notice.

This can lead to situations where it may take time before realizing there is a simple type error!

So IMO, at least this should be mentioned, or a notice should be thrown.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-03-22 01:58 UTC]
-Status: Open +Status: Feedback
 [2018-03-22 01:58 UTC]
You sure? Because on Windows I see
> Warning: file_put_contents(...): failed to open stream: No such file or directory

Maybe you have warnings hidden? Check your display_errors, error_log, and error_reporting settings.
 [2018-03-22 02:59 UTC] briethings at gmail dot com
-Status: Feedback +Status: Closed
 [2018-03-22 02:59 UTC] briethings at gmail dot com
Sorry, you're totally right!
However I had error_reporting set to E-ALL, but I fell into a stupid but interesting pitfall (so I'll report it to user-notes instead of bug!).

Here it is: the $filename arg was coded as $path.$file, with say $path = 'path/to' and $file = 'file.txt'.
You see that $path lacks its ending "/", so the resulting full path was 'path/tofile.txt': so file_put_contents() didn't fail but created the 'tofile.txt' in the *parent* directory of the one where I expected it, making me believe that nothing had happened!

Again, sorry for the false alert, and thanks for your vigilance.
 [2018-03-22 03:02 UTC]
-Status: Closed +Status: Not a bug -Package: Documentation problem +Package: Filesystem function related
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Feb 28 11:01:24 2021 UTC