file_put_contents
Writes data to a file. This is the preferred way to write data to a file as it handles opening, writing, and closing automatically.
Parameters
main.php
Return Values
int|false
Returns the number of bytes written to the file, or false on failure
On success:Number of bytes successfully written to the file
On failure:false if unable to write to file or file cannot be created
Notes
- Automatically handles opening and closing the file
- Creates the file if it doesn't exist (with proper permissions)
- Use FILE_APPEND flag to append data instead of overwriting
- Use LOCK_EX flag to acquire exclusive lock during writing
Changelog
- 5.0.0: Function was introduced
- 5.1.0: Added support for LOCK_EX flag
- 8.0.0: context parameter is now nullable
Related Functions
file_get_contents
File SystemReads entire file into a string. This is the preferred way to read the contents of a file into a string.
filereadhttp
pathinfo
File SystemReturns information about a file path including directory, basename, extension, and filename.
filepathextension