6

I'm using the PHP function file_put_contents() to put some content into a txt file. The example in the docs doesn't finish using fclose(), should I close the file or it's not necessary?

I'm doing this:

        $root = $_SERVER['DOCUMENT_ROOT'];
        $log = $root.'/logs/logsContenido.txt';
        $agregadoLog = "texto a agregar";
        file_put_contents($log, $agregadoLog, FILE_APPEND | LOCK_EX);

And just that. I don't close anything.

Should I rather do something like:

            $root = $_SERVER['DOCUMENT_ROOT'];
            $log = $root.'/logs/logsContenido.txt';
            $agregadoLog = "texto a agregar";
            $file = file_put_contents($log, $agregadoLog, FILE_APPEND | LOCK_EX);
fclose($file);
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
  • 1
    It's impossible to use `fclose` on `file_put_contents`, at least in userland environment. There is no stream to close. – Andrei Dec 05 '16 at 15:20
  • 5
    Did you go through all of it? http://php.net/manual/en/function.file-put-contents.php - One instance shows *"It should be obvious that this should only be used if you're making one write, if you are writing multiple times to the same file you should handle it yourself with fopen and fwrite, the fclose when you are done writing."* and *"This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file."* – Funk Forty Niner Dec 05 '16 at 15:20
  • *"This function returns the number of bytes that were written to the file, or FALSE on failure."* – What do you expect to call `fclose` *on*…? – deceze Dec 05 '16 at 15:32

2 Answers2

20

No, you should not/cannot. file_put_contents takes care of opening the file, writing the contents, and closing the file. In fact it does not expose any handle to you which you could close even if you wanted to.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I always trust the manual, but I noticed that if I run this PHP-CLI script: $test1 = get_resources(); file_put_contents($log, $msg, FILE_APPEND | LOCK_EX); $test2 = get_resources(); there is 1 resource more in $test2 (besides stdin, stdout, stderr that are there before and after). The manual says that get_resources() should only return active resources. I don't mean to say that file_put_contents is at fault, could be either xdebug that is failing me or get_resources()? – wlf Oct 30 '20 at 20:04
  • nevermind it, if i do: $resources = get_resources(); foreach($resources as $resource){fclose($resource);} i get "fclose(): supplied resource is not a valid stream resource". still dunno why get_resources() shows it – wlf Oct 30 '20 at 20:34
  • I have a case where I do a file_get_contents, and write them to the screen, then modify them and file_put_contents the revised contents and follow it immediately with a file get contents of the same file and write them to the screen. Contents are not changed then the program ends. When I run it again, the contents were updated making it appear that the file may not have fully closed until the program ended. I do not know for sure what is or is not happening, but if enough people observe similar situations, it might make it worth my while to dig deeper. – Ted Cohen Aug 17 '23 at 14:44
1

In file_put_contents, PHP handles it for you. So there is no need to fclose it.

But if you are using an handle like fopen, then you need to close it since you'll leave the file open during the entire exection of the script. Oh and also it is a good practice to close the handle once you are done with it

Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • See [here another response:](https://stackoverflow.com/questions/3938534/download-file-to-server-from-url/) from @alex. He uses: `file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));` and he does not say if `fclose(...)` should be used. But I had unpredictable problems with **HTTP request failed!** errors. – vlakov Oct 04 '17 at 22:54