php

Create new file and write data to it using PHP

<?php
    $new_file = fopen("filename.json", "w") or die("File can not be opened");
    $data = '{"firstname": "Hello", "lastname": "World"}';
    fwrite($new_file, $data);
    fclose($new_file);
?>

To create a new file in PHP you can use fopen() function where you can pass file path along with file name as a parameter and another parameter as 'w' to make it writable.

You can also write to this newly created file using fwrite() function which takes file instance and data which needs to be written on file.

Don not forget to close file after write data to it to release memory.

Was this helpful?