SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 ()?

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    <?php
    ...
    $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.
    Last edited by ronalds; Nov 1, 2012 at 10:37. Reason: added script type

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    <?php
    //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
    
    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And to save the image to a directory and write to json, how does? That is my biggest question. = /

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    PHP, has pretty good manual with examples.
    Dealing with uploaded files http://php.net/manual/fr/function.move-uploaded-file.php
    encoding data to json http://php.net/manual/en/function.json-encode.php
    and writing data to file http://php.net/manual/en/function.fwrite.php

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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');

    include('class/resize-class.php');

    $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
    file_put_contents(
    'uploads/' . $arquivo,
    file_get_contents('php://input')
    );
    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)";
    exit;
    }


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

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

    fclose($handle);

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

    }

    $i++;

    exit();

    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •