【问题标题】:mysql blob image displaying with php/html用 php/html 显示 mysql blob 图像
【发布时间】:2012-08-08 15:49:35
【问题描述】:

经过两天的搜索,我来到这里希望找到解决方案。

我实际上是在尝试将文件存储在数据库(mysql)中,我知道它有多糟糕,但我们实际上需要对应用程序进行封装,因此我们无法将它们与源一起存储。

我使用 3 个文件:

  • 我的表单(经典 html/php 文件)
  • 一个 php 文件,它处理表单中的数据以便将它们放入数据库中
  • 另一个 php 文件,以便从数据库中获取图像,发送带有GET 的 ID

问题是我无法显示我的图像。

表格->

<?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>

表单的数据在哪里处理->

<?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);
?>

这里是显示器->

<?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]);
?>

【问题讨论】:

  • 先尝试注释掉headers,看看返回的数据是否正常,或者正在生成其他输出
  • 如果我将它们注释掉,我会得到一个二进制数据的原始页面
  • 我认为这没问题,因为 BLOB 是 BINARY LARGE OBJ
  • 您是否检查了正在发送的标头?你确定$pic[0] 包含valid MIME type
  • 另外,您确定您的显示文件没有发送前导或尾随空格吗?

标签: php mysql image blob


【解决方案1】:

好的,我找到了解决方案,使用数据 URI。如果您遇到麻烦,请访问此处了解更多信息:php: recreate and display an image from binary data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-09
    • 2013-12-31
    • 2014-03-19
    • 1970-01-01
    • 2011-08-10
    • 2017-01-26
    相关资源
    最近更新 更多