Save Image Upload from a PHP variable

0

How to capture an image that comes from an input file type of HTML, in a PHP variable? For example with a text input it works like this:

$vnome = ($_POST["f_nome"]);
echo $vnome;

<input type="text" name="f_nome">

How to do the same with an image? Is it possible to save image in a PHP variable?

    
asked by anonymous 25.09.2017 / 17:07

1 answer

1

The server along with PHP before running your script has already parse the PAYLOAD and saved the image to the ./tmp folder set by php.ini at upload_tmp_dir ", then you can upload normally and use file_get_contents , a basic example would be:

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="arquivo" multiple>
<button type="submit">Upload</button>
</form>

And php would be:

<?php

if (empty($_FILES['arquivo']['tmp_name'])) {
     die('Erro no upload');
}

//Pega os dados do upload
$dados_da_imagem = file_get_contents($_FILES['arquivo']['tmp_name']);

If you want to display on the screen you will need to use data URI scheme a>, something like:

$dados_da_imagem = file_get_contents($_FILES['arquivo']['tmp_name']);

echo '<img src="data:image/gif;base64,', base64_encode($dados_da_imagem),'">';

Now if your intention is to preview the image before completing the upload, I recommend that you do this on the front-end before sending the POST, example of JavaScript tools:

25.09.2017 / 17:24