2012-04-19 4 views
0
여기

내가 시도하고 데이터베이스에서 이미지를 검색하는 데 사용하고있는 코드 표시되지 : (확실하지 :가져 오는 이미지 이미지가

<?php 
if($id) 
{ 
    //please change the server name username and password according to your mysql server setting 
    $mysql_server="localhost"; 
    $mysql_username="myuser"; 
    $mysql_password="mypass"; 
    $mysql_database="mydb"; 
    //connect to database using above settings 
    @MYSQL_CONNECT("localhost",$mysql_username,$mysql_password); 
    @mysql_select_db("mydb"); 
    //select the picture using the id 
    $query = "select bin_data,filetype from todo where id=$id"; 
    //execute the query 
    $result = @MYSQL_QUERY($query); 
    //get the picture data which will be binary 
    $data = @MYSQL_RESULT($result,0,"bin_data"); 
    //get the picture type. It will change according to file extension it may be either gif or jpg 
    $type = @MYSQL_RESULT($result,0,"filetype"); 
    //send the header of the picture we are going to send 
    Header("Content-type: $type"); 
    //send the binary data 
    echo $data; 
}; 


?> 

대신의 요청 된 이미지를 표시,이 아이콘을 표시합니다 여기

http://i.imgur.com/bo6Jg.png 내 테이블의 모든 열이 무엇인지 당신이)를 호출 ... : http://i.imgur.com/PuWvl.png

내가 모든 일을하고있어 꽤 확실 해요 바로 ... 무슨 일이 일어나고 있는지 확실하지. 누구 한테 도움을 줘? 미리 감사드립니다!

+0

그 사악한 @ 표지판을 제거하십시오! ?> –

+0

mysql 함수 이름은 대소 문자를 구별합니다. 대/소문자는 대문자로 쓰일 수 있습니다. 오류를 숨기려면 크래커가 필요합니다. 또한 '$ id'도 비어 있습니다. 그래서 결코 멀리까지는 가지 않을 것입니다. 대신'isset ($ id) && is_numeric ($ id)'를 사용해야합니다. –

+0

PHP에서 @LawrenceCherone 함수 이름은 대소 문자를 구분하지 않지만, 여전히 그들이 취급하는 것처럼 취급해야한다고 생각합니다. –

답변

1

IMO 유일한 상수는

어쨌든이 시도, (나는 기꺼이 그들이 위가 될 수 몰랐다 THO) 대문자이어야한다 :

<?php 
$file_not_found = '../not_found_image.jpg'; 
//Get the id param from GET else null 
$id = (isset($_GET['id']) && is_numeric($_GET['id']))?$_GET['id']:null; 

if($id != null) { 
    $mysql_server="localhost"; 
    $mysql_username="myuser"; 
    $mysql_password="mypass"; 
    $mysql_database="mydb"; 
    //Connect to database using above settings 
    mysql_connect($mysql_server,$mysql_username,$mysql_password) or die(mysql_error()); 
    mysql_select_db($mysql_database) or die(mysql_error()); 
    //Select the picture using the id 
    $query = "SELECT `bin_data`, `filetype` FROM todo WHERE id=".(int)mysql_real_escape_string($id)." LIMIT 1"; 
    //Execute the query 
    $result = mysql_query($query); 

    //Found 
    if(mysql_num_rows($result)==1){ 
     //Get the picture data which will be binary 
     $data = mysql_result($result,0,"bin_data"); 
     //Get the picture type. It will change according to file extension it may be either gif or jpg 
     $type = mysql_result($result,0,"filetype"); 
     //Send the header of the picture we are going to send + cache 
     header('Cache-Control: private, max-age='.(60*60*24*365)); 
     header('Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365)); 
     header("Pragma: private"); 

     header('Content-Type: '.$type); 
     header('Content-Length: ' . strlen($data)); 
     //Send the binary data 
     echo $data; 

    }else{ 
    //Not found 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Type: image/jpeg'); 
    header('Content-Length: ' . filesize($file_not_found)); 
    readfile($file_not_found); 
    } 

}else{ 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Type: image/jpeg'); 
    header('Content-Length: ' . filesize($file_not_found)); 
    readfile($file_not_found); 
} 
?> 
+0

Worked! 감사. – Alex

0

이 시도 ..

header("Content-type: image/gif"); 
$expires = 60*60*24*14; 
header("Pragma: public"); 
header("Cache-Control: maxage=".$expires); 
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT'); 
error_reporting(0); 
require_once "class/dbconn.php"; 
$id=$_GET['id']; 

$sql="select thumbimage from image where img_id=$id"; 
$rs=mysql_query($sql) or die (mysql_error()); 
$row =mysql_fetch_array($rs,MYSQL_BOTH); 
$data = $row[0]; 
print $data; 
관련 문제