php
Write data to file using PHP
<?php
$filePath = 'path/to/file/';
$fileData = 'Hello world';
file_put_contents($filePath, $fileData); // WRITE FRESH DATA TO FILE
file_put_contents($filePath, $fileData, FILE_APPEND | LOCK_EX); //APPEND DATA AT THE END WITHOUT DELETING PREVIOUS DATA
?>
Writing data to a file can be performed in PHP by using its inbuilt function file_put_contents(). It takes two required parameters as file path and file content to be written on it. The detailed description is as below.
file_put_contents($filePath, $fileData, FILE_APPEND | LOCK_EX);
$filePath(required): This is a required parameter which is the path of the file where you want to write your data.
$fileData(required): This parameter contains the file content which is to be written on file. This is also a required parameter.
FILE_APPEND: If you want your content to be append on the end of file without deleting your previous data you can pass this parameter. If you want to lock file until it is written you can use LOCK_EX keyword with this.
Was this helpful?
Similar Posts