file_put_contents
Description
The file_put_contents of Filesystem for PHP write data to a file.
Syntax
file_put_contents( string $filename, mixed $data, int $flags = 0, ?resource $context = null ): int|false
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
Parameters
filename
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.
If data is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream().
You can also specify the data parameter as a single dimension array. This is equivalent to file_put_contents($filename, implode('', $array)).
flags
The value of flags can be any combination of the following flags, joined with the binary OR (|) operator.
Value | Name | Description |
---|---|---|
1 | FILE_USE_INCLUDE_PATH | Search for filename in the include directory. See include_path for more information. |
2 | LOCK_EX | Acquire an exclusive lock on the file while proceeding to the writing. In other words, a flock() call happens between the fopen() call and the fwrite() call. This is not identical to an fopen() call with mode "x". |
8 | FILE_APPEND | If file filename already exists, append the data to the file instead of overwriting it. |
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.
WARNING: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Examples
1 · filename data
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = "hello"; $return = file_put_contents($filename, $data); echo $return; ?>
5
2 · flags · 0
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = "hello"; $flags = 0; $return = file_put_contents($filename, $data, $flags); echo $return; ?>
5
3 · flags · LOCK_EX
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = "hello"; $flags = LOCK_EX; $return = file_put_contents($filename, $data, $flags); echo $return; ?>
5
4 · flags · FILE_APPEND
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = " world"; $flags = FILE_APPEND; $return = file_put_contents($filename, $data, $flags); echo $return; ?>
6
5 · context
<? $options = array("http" => array("method" => "POST")); $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = "hello"; $flags = 0; $context = stream_context_create($options); $return = file_put_contents($filename, $data, $flags, $context); echo $return; ?>
5
6 · Append · file_get_contents
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = file_get_contents($filename); $data .= " world"; $return = file_put_contents($filename, $data); echo $return; ?>
11
7 · Append · FILE_APPEND
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $data = " world"; $flags = FILE_APPEND | LOCK_EX; $return = file_put_contents($filename, $data); echo $return; ?>
6
Links
Related
Filesystem
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- file
- file_exists
- file_get_contents
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- ftruncate
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath
- realpath_cache_get
- realpath_cache_size
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink