PHP how to pick up image, in hexa format straight from MSSQL?

Asked

Viewed 993 times

0

I’m making a system with PHP and I have already managed to place an image or word file, directly, inside the MSSQL, SQLSRV, without having to put them in a folder, but I can’t find them and display them in normal format. Does anyone know how I can do this? This data recovery thing? Insertion code in the bank:

$dataHex = bin2hex($nome_final);

$imagedata = file_get_contents($nome_final);

$base64 = base64_encode($imagedata);


$sql = "INSERT INTO [RDO].[dbo].[documentosSub] (id_fun, nome_documento, hexa, base64) VALUES (?,?,?,?)";

$params = array($id_fun, $nome_final, $dataHex, $base64);

$stmt = @sqlsrv_query( $conn, $sql, $params);

$pasta = $_UP['pasta'];
$pastaP = explode('/', $pasta);
$pasta = $pastaP[0];


if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $_UP['pasta'] . $nome_final)) {

header("Location: teste.php?pasta=$pasta&id_fun=$id_fun&cc=$cc");
} else {
echo "Não foi possível enviar o arquivo, tente novamente";
}

Code for display:

$sql = "SELECT * FROM [RDO].[dbo].[documentosSub] WHERE id_fun='$id_fun' ";
$stmt = @sqlsrv_query( $conn, $sql);
?>
<table align="center" width="400" border="1">
    <tr>
        <td align="center"><b>Id</b></td>
        <td align="center"><b>Nome</b></td>
        <td align="center"><b>Download</b></td>
        <td align="center"></td>
    </tr>
<?php 
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $id_documento = $row['id_documento'];
            $nome_documento = $row['nome_documento'];
            $hexa = $row['hexa'];
            $base = $row['base64'];
            $binary_string = pack("H*" , $hexa);
            $base64 = base64_decode($base);

            $arquivo = $prefixo.'/'.$pasta.'/'.$nome_documento;
            $imgP = explode(".", $arquivo);

?>

    <tr>
        <td align="center"><?php echo $id_documento ?></td>
        <td><?php echo $nome_documento ?></td>
        <td align="center"><a href="<?php echo $arquivo ?>"><?php echo $nome_documento ?></a></td>
        <!--<td><?php echo $base64 ?></td>-->
    </tr>
    <?php
    }
    ?>
</table>
  • The problem is that I am not using version 5.4 of php that supports hex2bin and the program I am running here is Vertrigo, similar to XAMPP, WAMPP, but there is no upgrade to php 5.4. I have a mega problem in hand. I would like to convert the image or doc to another format for the bank and then undo the conversion?

  • @Gustavosevero Edit the question stating this reason not to use hex2bin, or your question will be closed as duplicate.

  • which version of php

  • Sorry Caffé, but in what way you want me to edit the question?

  • Hi Otto, it’s version 5.3

  • 1

    it is interesting to always put as much information as possible so that we can help....

  • 1

    Not to miss the joke .... Gustavo I think you are too Severo

  • I was able to convert the image, before saving in the bank, as Base64, but also I am not able to recover it.

  • Got how ? tried with the answer I sent ?

  • 1

    Simply, doing this: $imagedata = file_get_contents($endname); $Base64 = base64_encode($imagedata);

  • then Voce threw her into the bank with Base64 .... makes the base64_decode

  • what you have in the $Base64 ?

  • You posted the code you are trying to use. Cool. But I think it is now unclear what problem you are facing.

  • Caffé, the problem of before, I can not make the image appear on the screen, or a link to the image.

Show 9 more comments

2 answers

3

As I understand your code, you are reading the image to the database, inserting the same encoded in Base64. Then you are doing the output image directly in your HTML.

This way it does not result in HTML to present an image accurate to make use of the element designated for this purpose, the tag <img/>.

Example

Tag <img/> with image in Base64:

<?php

// gatafunhos que vem da base de dados em Base64
$minhaImagemEmBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

// output da tag HTML com os gatafunhos em Base64
echo '<img src="data:image/jpeg;base64,'.$minhaImagemEmBase64.'"/>';

?>

See example in Jsfiddle

We have a red dot in Base64 to be presented on the page instead of establishing a link to an image type file.

0

You can use 'pack' :

$binary_string = pack("H*" , $hex_string);
  • Otto, it didn’t work out so well. I just return the name of the image!

  • How can I post the code here, with the correct formatting, identation?

  • edits the question puts the question that I then edit and format

  • Okay, I posted the code.

  • for what I saw you made $Base64 = base64_encode($imagedata); does the opposite $Base64 = base64_decode($imagedata); to display

  • it’s still very strange

  • Why? It was supposed to work?

  • should .... I’m thinking of a way to make it better

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.