webCOMAND

util::file_put_contents()

Convenience method to create a file, including any preceding directories in the file path.

After creating any missing directories, PHP's built-in file_put_contents is called.

Prototype

int file_put_contents(string $path, mixed $data, mixed $flags = 0, mixed $context = NULL)

Parameters

  • path - Path to the file where to write the data.
  • data - The data to write. Can be either a string, an array or a stream resource.
  • flags - Any combination of flags excepted by file_put_contents, joined with the binary OR (|) operator.
  • context - A valid context resource created with stream_context_create().

Return

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

Example

$filename = 'dir/sub_dir/test.txt';

// create a file and any directories that do not exist in the path
$ok = \io_comand_file\util::file_put_contents($filename, '123');
if(!$ok) {
    \comand::log_error("Could not create file: $filename");
}