file put contents code example

Example 1: write to file php

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, "Content to write to file");
fclose($myfile);

Example 2: file_put_contents php json file

$data[] = $_POST['data'];

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData);

Example 3: php file put content

<?php
$data = ["[email protected]" => ["more" => ["yes" => "More"]]];

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData);

Example 4: php file_put_contents

file_put_contents ($filename, $data, $flags = 0, $context = null): int