Save file uploads in json

Friends, I created a system to upload multiple images but I need to write everything in a json file instead of a database.

Can you give me an example of how to do this? It is with fopen ()?

My best guess would be to base64 encode the images and then store those strings in json file, but storing files in json or database doesn’t seem like the best solution.

$type = pathinfo($filename, PATHINFO_EXTENSION);
$data = fread(fopen($filename, "r"), filesize($filename));
$encoded_data =  'data:image/' . $type . ';base64,' . base64_encode($data);

Usual approach would be to store file itself on disk and in json/db store only it’s attributes, like file size, height, width, name, type, etc.

Sorry for my bad English. Actually I want to store the file json only the id and the path of the image and the title of the gallery. The images would be saved in a folder, normally.

Do not quite understand this script you gave me? The path to the file json is that part?

That is just a php snippet to get image encoded in base64 string, that can be stored in json file and afterwards directly used as src in img tag.
To store image attributes in json I would use approach like this:

//here you have your image attributes
$new_image = array('name' => 'image name', 'gallery_title' => 'new Gallery');

//read and decode data from your json file to array $contents

//add  new image data to array
$contents[] = $new_image;

//encode to json and write data back to file


And to save the image to a directory and write to json, how does? That is my biggest question. = /

PHP, has pretty good manual with examples.
Dealing with uploaded files
encoding data to json
and writing data to file

I want the path of an upload multiple images to be written to a file json, but the recording is not working. I did so, but multiple objects “images” are created. Would that all images were recorded on the same object:

// Informações para conexão
$host = ‘localhost’;
$usuario = ‘root’;
$senha = ‘’;
$banco = ‘kurt’;
// Realizando conexão e selecionando o banco de dados
$conn = mysql_connect($host, $usuario, $senha) or die(mysql_error());
$db = mysql_select_db($banco, $conn) or die(mysql_error());
// Definindo o charset como utf8 para evitar problemas com acentuação
$charset = mysql_set_charset(‘utf8’);


$fn = (isset($_SERVER[‘HTTP_X_FILENAME’]) ? $_SERVER[‘HTTP_X_FILENAME’] : false);

function extensao($file_name){
$ext = explode(‘.’, $file_name);
$ext = array_pop($ext);
return strtolower($ext);

function criptografa($file_name){
$ext = explode(‘.’, $file_name);
$ext = array_pop($ext);
$nome = md5(uniqid(rand(), true)) . ‘.’ . strtolower($ext);
return $nome;

if ($fn) {

$arquivo = criptografa($fn);

// AJAX call
‘uploads/’ . $arquivo,
echo “$arquivo uploaded”;

$resizeObj = new resize(‘uploads/’ . $arquivo);
$resizeObj -> resizeImage(200, 200, ‘crop’);
$resizeObj -> saveImage(‘uploads/thumb_’ . $arquivo, 100);

$galeria = $_SERVER[‘HTTP_X_GALERIA’];

header(‘Content-Type: application/json;charset=utf-8’);
$filename = ‘test.json’;

$i = 0;
$busca = array();

$busca[$i][‘id’] = rand(1, 99);
$busca[$i][‘galeria’] = $galeria;
$busca[$i][‘imagem’] = criptografa($fn);

foreach($busca as $string)

$grava[“imagens”] = $string;

$somecontent = json_encode($grava);

if (is_writable($filename)) {

if (!$handle = fopen($filename, ‘a’)) {
echo “Não foi possível abrir o arquivo ($filename)”;

if (fwrite($handle, $somecontent) === FALSE) {
echo “Não foi possível escrever no aqruivo ($filename)”;

echo “Sucesso. Escreveu ($somecontent) no arquivo ($filename)”;


} else {
echo “Impossível escrever em $filename”;