PHP.nl

file_put_contents

file_put_contents

Write data to a file

 **file_put_contents** string $filename mixed $data int $flags  $context

This function is identical to calling , and successively to write data to a file. fopen``fwrite``fclose

If does not exist, the file is created. Otherwise, the existing file is overwritten, unless the flag is set. filename``FILE_APPEND

filenamePath to the file where to write the data.

data The data to write. Can be either a , an or a resource. string``array``stream

   If  is a  resource, the
   remaining buffer of that stream will be copied to the specified file.
   This is similar with using .
  `data``stream``stream_copy_to_stream`


   You can also specify the  parameter as a single
   dimension array. This is equivalent to
   .
  `data``file_put_contents($filename, implode('', $array))`

flags The value of can be any combination of the following flags, joined with the binary OR () operator. flags``|

context A valid context resource created with . stream_context_create

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

Voorbeeld: Simple usage example

<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

Voorbeeld: Using flags

<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>

fopen``fwrite``file_get_contents``stream_context_create