SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 27 of 27
  1. #26
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Friends, I created a system to upload multiple images but I need to write everything in a json file instead of a database.

    I need the id, the id and name of the gallery and the image file is written to the json file, but multiple objects "images" are written in the file. The data should be recorded within a single object "images".

    My script:

    Code:
    // 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(); 
    
    }
    Write this in the file json (2 images):

    Code:
    {"imagens":[{"id":51,"galeria":"50","imagem":"43e4ff5357ccb77c28329758a56bfc92.gif"}]}{"imagens":[{"id":55,"galeria":"50","imagem":"ab8ed241aac07b57ff276e533e65a9be.png"}]}

  2. #27
    SitePoint Zealot
    Join Date
    Jul 2012
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is my impression or a response has been erased? = /


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
  •