2012-08-08 3 views
1

2 일간의 검색을 통해 나는 해결책을 찾기를 희망한다.PHP/html로 표시되는 mysql blob 이미지

실제로 데이터베이스 (mysql)에 파일을 저장하려고합니다. 얼마나 나쁜지는 잘 압니다 만 실제로 응용 프로그램의 풋 프린트가 필요하므로 소스와 함께 저장할 수 없습니다.

나는 3 개 개의 파일을 사용

  • 내 양식 (고전 HTML/PHP 파일)
  • 위해 양식에서 데이터를 처리하는 PHP 파일은 DB
  • 다른 PHP 파일에 넣어합니다 DB에서 이미지를 가져오고 ID를 보내려면 GET

문제는 내 이미지를 표시 할 수 없다는 것입니다.

형상 - 데이터 양식을 형성>

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

processed- 있습니다 그리고 여기에는 디스플레이 ->

<?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]); 
?> 
입니다
+0

먼저 데이터가 반환되는지 확인하기 위해 헤더를 주석 처리하거나 다른 출력을 생성하려고합니다. – Waygood

+0

내가 이진 데이터 –

+0

의 원시 페이지를 가지고 있고 BLOB이 BINARY LARGE OBJ –

답변

관련 문제