Sitemap / Advertise




How to create a JSON file and upload it automatically to the client-server in PHP



Read Later




Read Later







In this tutorial, I will show you how to create a JSON file with a given string and upload the file to the client-server automatically without needing to click a classic verification button in PHP.

You can inspect a web application named JSON File Creator, that utilizes the same method, from the link below. Please sign in before inspecting the mentioned application.

Go to JSON File Creator.

Introducing JSON(1)


JSON is built on two structures:




In JSON, they take on these forms:

An object is an unordered set of name/value pairs. An object begins with { left brace and ends with } right brace. Each name is followed by : colon and the name/value pairs are separated by , comma.

An array is an ordered collection of values. An array begins with [ left bracket and ends with ] right bracket. Values are separated by , comma.




Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.




Create a function named JSON_File_Creator with two parameter - data string and file name.

Using the explode() function, split the data string to find every main object.

For each main object (seperated by '/'):

Define add and new line arrays.

Get the main object's name and variables.

For each object in the main object (seperated by ';'):

Using the explode() function, split every variable that of the current object (seperated by ',').

For each variable:

Get the name of the variable and the value of the variable.

Add the new variable to the new line array with its name.

Using the array_push() function, add the new line array including the variables of the current object to the add array as a new array member.

Blank the new line array.

With the main object name, add the add array to the content array.

Open a file named as the predefined file name.

Using the json_encode() function, convert the content array to the JSON format and write the encoded array to the recently created file.

Close the file.

Define the file headers.

Upload the file to the client-server by executing the readfile() function.

To prevent user disruption, use the ignore_user_abort(true) function.

To delete the file, use the unlink() function.

Define the content array and the data string.

Note: To avert the Undefined Offset 1 error when using the explode() function, add the given delimiter to the end of the string ("$string[delimiter]").

function JSON_File_Creator($data_string, $file_name){
	$data = explode("/", $data_string);
	forEach($data as $main_string){
		$add = [];
		$new_line = [];
        $object_name = explode(":", $main_string)[0];
        $object_values = explode(":", "$main_string:")[1];
		forEach(explode(";", $object_values) as $objects){
			$object_variables = explode(",", $objects);
			forEach($object_variables as $new_variable){
				$name = explode("=", $new_variable)[0];
				$value = explode("=", "$new_variable=")[1];
				$new_line[$name] = $value;
			array_push($add, $new_line);
			$new_line = [];
		$content[$object_name] = $add;
	$file = fopen($file_name, 'w');
    fwrite($file, json_encode($content));
    header('Content-Description: JSON File Creator');
    header('Content-Disposition: attachment; filename="'.$file_name);

$content = [];
$string = "test:test=test,test=test;new=new,new=new/Books:id=001,name=To Kill a Mockingbird,author=Harper Lee;id=002,name=The Count of Monte Cristo,author=Alexandre Dumas;id=003,name=The Secret Life of Bees,author=Sue Monk Kidd";
JSON_File_Creator($string, "test.json");




(1) https://www.json.org/json-en.html