Search name file uploaded by json

7

I'm using a tool to crop an image sent by the user. When the crop is done, the file is saved in the folder and sends the response by json_encode .

ItturnsoutthatIwanttograbonthe"name_file" returned by json, and save it in DB when I'm submitting the form. My form:

<form action="runewcode?runid=ew-assinaturas001" method="post" enctype="multipart/form-data">
(...)

<div class="col-lg-6">
    <div id="cropContainerEyecandy" style="height: 300px;"></div> <!-- ONDE É FEITO O CROP DA IMAGEM -->
</div>

(...)
</form>

I'm trying something like this to get name_file :

<?php 
if(isset($_POST['name_file']))
{
     //GUARDA NAME_FILE NA BD
}
?>
    
asked by anonymous 07.10.2015 / 11:22

4 answers

1
<?php

//Exemplo
$json = '[{"status": "success","name_file": "imagemTal","url": "www.tttt.com.br/imagemTal"}]';

$jasondata =json_decode($json,true);

//Aqui você captura acessa um valor específico
echo $jasondata[0]['name_file']; exit;

echo "<pre>"; print_r($jasondata); exit;

  //  Resultado:    
  //  imagemTal


    ?>
    
07.10.2015 / 15:34
0

Your form has the multipart/form-data attribute, which is only used when you want to send / move a file to the server. This attribute is only valid for inputs of type type="file" , and to receive the values of this input on the server side, $_FILES is used instead of $_POST that is used for other types. p>

I'm still a bit confused, since in the image above you already have the request made, and even a return, I just do not understand this part:

  

"I'm trying something like this to get the name of the image:"

But responding to what I perceived to be the problem, since you already have the request made, and you just need to read the value of the parameters contained in it, I have this example, self-explanatory.

Example:

<?php

// header("Content-Type: text/plain");
// Comentei o header para poder exibir a formatação dos echo's
$arr = array("status" => "success",
             "name_file" => "croppedImg_11647",
             "url" => "..\/publicfiles\/croppedImg_11647.png"
            );

echo "<h2>Codificar com \"json_encode()\"</h2>";            
$encode = json_encode($arr);            
echo $encode;

echo "<h2>Decodificar com \"json_encode()\"</h2>";
$decode = json_decode($encode);
echo "<p><strong>Saida (Objecto):</strong></p>";
var_dump($decode);

echo "<p><strong>Valores do objecto 1:</strong></p>";

echo $decode->status . "<br/>";
echo $decode->name_file . "<br/>";
echo $decode->url . "<br/>";

echo "<p><strong>Valores do objecto 1\":</strong></p>";
foreach($decode as $id=>$valor){
    echo "<b>{$id}</b> : " . $valor . "<br/>";      
}

echo "<p><strong>Converter os valores do objecto para uma array :</strong></p>";
foreach($decode as $id=>$valor){
    $array[$id] = $valor;
    // Ou da forma simplificada
    // $array = $valor; 
}

var_dump($array);

?>
    
07.10.2015 / 12:14
0

If you already have an exit in json_encode() means that there has already been upload processing, it does not make much sense to recreate an upload form to send the same thing twice. Now what would make sense is to capture this JSON output in javascript rather than PHP because you have already submitted image processing in PHP and already have output in JSON, you agree.

If you want to treat within PHP and save some data before exiting in JSON, you need to find out where that output is json_encode($array_saida) get that variable: $array_saida , and find the $array_saida['name_file'] key before moving on to encode and save the data in the database.

However, I believe that you may be inside a form and want to load only the reference and display this image inside a form or something before executing the submission of a save form, however, there should be no co- % on this form.

The way to capture your JSON is just by returning the same ajax method that cropped this image and generated this JSON output. As I do not know your method, I'll give you an example of a return based on this API: link

 $.ajax('/path/to/upload', {
              method: "POST",
              data: formData,
              processData: false,
              contentType: false,
              success: function (data) {
                    var e = jQuery.parseJSON(data);
                      $('#sua_imagem').attr('src', e.url); 
                      $('#img').val(e.name_file); 
               },
              error: function () {
                      console.log('Ocorreu um erro no processo!');
              }
 });

And in your HTML form:

<form action="?salvar" method="post" name="salvamento">
  <img src="undefined.jpg" id="sua_imagem" width="180" height="180" border="0">
  <input type="hidden" id="img" name="imagem"><br>
  <input type="submit" value="Enviar">
</form>
    
07.10.2015 / 15:17
0

In php, to get the name of a file sent via post by form, use the following:

$_FILES['name_do_input_file']['name'] //retorna o nome original do arquivo
$_FILES['name_do_input_file']['tmp_name'] // retorna o nome/destino do arquivo temporário que foi criado como upload no seu servidor

To copy the newly uploaded file, use:

copy($_FILES['name_do_input_file']['tmp_name'], "diretorio/subdiretorio/".$_FILES['name_do_input_file']['name'])

In this example copy, you can change $ _FILES ['name_of_input_file'] ['name'] to any other name that you want the file to have, but you will need to handle the

    
07.10.2015 / 19:27