Search code snippets, questions, articles...

Create new file and write data to it using PHP

    $new_file = fopen("filename.json", "w") or die("File can not be opened");
    $data = '{"firstname": "Hello", "lastname": "World"}';
    fwrite($new_file, $data);
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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?