1

After two days of search I come here with the hope of finding a solution.

I am actually trying to store files in the database (mysql), I know how bad it is, but we actually need to do a footprint of the application, so we can't store them with the sources.

I use 3 files:

  • my form (classic html/php file)
  • a php file who process data from form in order to put them into the DB
  • another php file in order to get an image from the DB, sending a ID with GET

The problem is that I can't display my image.

the form->

<?php
    echo ('<img src="profil_pic_display.php?uid_owner=' . $dCookie['uid']. '">'); //The display line
    ?>
    <form method="POST" action="upload_profil_pic.php" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
        Selectionnez une photo: <input type="file" name="avatar"/>
        <br/>
        <br/>
        <input class="ui-button" type="submit" value="Envoyer"/>
        <span><small>Formats acc&eacute;pt&eacute;s: .png/.jpeg/.jpg/.gif | Taille max: 250ko</small></span>
    </form>

Where the data form the form are processed->

<?php
require_once ('bdd.php');
require_once ('cookie.php');

$dCookie = check_cookie(1);
$maxSize = 250000;
$extensions = array('.png', '.gif', '.jpg', '.jpeg');

// On verifit que le transfert s'est bien deroule et donc que l'image est bien stocke     en tmp
if (!isset($_FILES['avatar']) || !is_uploaded_file($_FILES['avatar']['tmp_name'])) {
    echo('Probleme de transfert');
    die();
}    

$pData = $_FILES['avatar'];
$pName = basename($pData['name']);
$pType = $pData['type'];
$pSize = filesize($pData['tmp_name']);

// Verification de l'extension et de la taille
if (!in_array(strrchr($pName, '.'), $extensions) || ($pSize > $maxSize)) {
    echo('Fichier non valide.');
    die();
}
// anihilation des ' ' et des accents.
$pName = strtr($pName, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',      'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$pName = preg_replace('/([^.a-z0-9]+)/i', '_', $pName);

//avant d'uplaoder une nouvelle image on supprime l'ancienne si il y a (en fonction de l'uid_owner
$reqCheckOldPic = $GLOBALS['bdd']->prepare('DELETE FROM num_profil_picture WHERE uid_owner   = :uid_owner;');
$reqCheckOldPic->bindParam(':uid_owner', $dCookie['uid']);
__log($dCookie['uid'], 'DELETE', array('QUERY' => $reqCheckOldPic->queryString, 'VALUE' =>    array(':uid_owner' => $dCookie['uid'])));
$reqCheckOldPic->execute();
$reqCheckOldPic->closeCursor();

// On prepare l'upload / recuperation du contenu binaire ET echappement du contenu binaire
$pBlob = file_get_contents($pData['tmp_name']);
$reqUpload = $GLOBALS['bdd']->prepare('INSERT INTO num_profil_picture (uid_owner,
                                                                    img_nom,
                                                                    img_taille,
                                                                    img_type,
                                                                    img_blob)
                                                            VALUE (:uid_owner,
                                                                    :img_nom,
                                                                    :img_taille,
                                                                    :img_type,
                                                                       :img_blob);');
$reqUpload->bindParam(':uid_owner', $dCookie['uid']);
$reqUpload->bindParam(':img_nom', $pName);
$reqUpload->bindParam(':img_taille', $pSize);
$reqUpload->bindParam(':img_type', $pType);
$reqUpload->bindParam(':img_blob', addslashes($pBlob));
__log($dCookie['uid'], 'INSERT', array('QUERY' => $reqUpload->queryString,
'VALUE' => array(
    ':uid_owner' => $dCookie['uid'],
    ':img_nom' => $pName,
    ':img_taille' => $pSize,
    ':img_type' => $pType,
        ':img_blob' => $pBlob)));
$reqUpload->execute();
$reqUpload->closeCursor();
header("Content-type: " . $pType);
echo ($pBlob);
?>

And here it is the display->

<?php

require_once('bdd.php');
require_once('cookie.php');


$reqGetPic = $GLOBALS['bdd']->prepare('SELECT img_type, img_blob FROM num_profil_picture WHERE uid_owner = :uid_owner;');
$reqGetPic->bindParam(':uid_owner', $_GET['uid_owner']);
$reqGetPic->execute();
$pic = $reqGetPic->fetch();

header("Content-type: " . $pic[0]);
header('Content-transfer-encoding: binary');
echo ($pic[1]);
?>
Mat
  • 202,337
  • 40
  • 393
  • 406
  • First try commenting out the headers to see if the data is returned okay, or other output is being generated – Waygood Aug 08 '12 at 16:00
  • If i comment them out i got a raw page of binary data –  Aug 08 '12 at 16:03
  • and this is ok in my opinion, since BLOB is BINARY LARGE OBJ –  Aug 08 '12 at 16:03
  • Have you checked the headers that are being sent? Are you sure that `$pic[0]` contains a [valid MIME type](http://en.wikipedia.org/wiki/Internet_media_type#Type_image)? – Gordon Bailey Aug 08 '12 at 16:15
  • Also, are you certain that there are no leading or trailing spaces being sent by your display file? – Gordon Bailey Aug 08 '12 at 16:16
  • raw page of binary data is good. View source, copy it and paste into a basic text editor then save as original (ie image.png) then view it in an images viewer. This should test if your headers are incorrect. You could also check it against the original (opened in a BASIC text editor) – Waygood Aug 08 '12 at 16:26
  • i have done the image.png thing, but nothing is displayed, i didnt know if it was normal or weird. I will try to compare them.
    And yes my in pic there is a valide MIME type, it contain 'image/jpeg' when i uplaod a jpg or jpeg for example
    –  Aug 09 '12 at 07:27
  • Ok when i open the two files (original image / uploaded image) there is a small difference of lenght :d where can this come from? –  Aug 09 '12 at 07:33
  • But in my main file in my main page i call my image diaply with this
    echo ('');
    –  Aug 09 '12 at 07:36
  • Maybe the difference can come from the addslashes() before putting the blob into the DB, or from the function file_get_content()? Just asking, i test with and without and no changes. Originale file size: 42,6 Ko (43 687 octets) Uplaoded file size: 43,6 Ko (44 668 octets) –  Aug 09 '12 at 07:44
  • Ok, I experimented a bit, changing addslashes()/fil_get_content() or fread params, here the first line result: original -> http://justbeamit.com/4c21a file_get_content + addslashes -> http://justbeamit.com/5f23b just file_get_content, no addslashes -> http://justbeamit.com/36586 with file_get_content or fread its exactly the same result. Without get_file_content or fread it only return the file path, as expected. –  Aug 09 '12 at 08:23
  • also tried to change the type in the Mysql's table, but varchar/blob/binary/text alway give the same result. –  Aug 09 '12 at 08:37
  • Ok, I just downloaded the blob file form MysqlDB, and its the same that the original. So the problem is coming form the display process. I dont think it come from my sql query with PDO. Maybe form the header, but MIME type is OK, i checked it. So maybe it come from the echo()? –  Aug 09 '12 at 09:48
  • Ok i found out the solution, using data URI. If you are in trouble, go here for more info! http://stackoverflow.com/a/2070625/1585121 have a good day. –  Aug 09 '12 at 11:58

1 Answers1

1

Ok i found out the solution, using data URI. If you are in trouble, go here for more info: php: recreate and display an image from binary data.

Community
  • 1
  • 1