5

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,062
  • 9
  • 34
  • 65
  • 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

19

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
  • 491,798
  • 79
  • 706
  • 853
  • 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
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
  • 7,842
  • 10
  • 47
  • 86
  • 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