2009-06-26 29 views
0

我知道你們中的一些人會說,這不是正確的方式,但即時通訊在完成一個應用程序的緊迫期限,截至目前我不能回去修改代碼來存儲目錄中的圖像。正確的方式上傳圖像到數據庫

現在這就是清除

的問題,我有我是通過鍵入此插入圖像到數據庫中。

(不介意類安全通話,所有正在做的是一些檢查的數據是有效的)

$filename = $security->secure($_FILES['imgschool']['name']); 
$tmpname = $security->secure($_FILES['imgschool']['tmp_name']); 
$imgsize = $security->secure($_FILES['imgschool']['size']); 
$imgtype = $security->secure($_FILES['imgschool']['type']); 
$school = $security->secure($_POST['school']); 


//begin upload 
if($imgsize > 0) { 
$handle = fopen($tmpname, "r"); 
$content = fread($handle, filesize($tmpname)); 
$content = addslashes($content); 

//code to add all this to database 
} 

變量$內容是圖像和所有得到的是和addslashes。我記得有人曾經提到過用base64做這件事,但我幾乎不記得它是如何寫成的。

這是怎麼了我從數據庫中的所有查詢調用圖像

一邊和諸如此類的東西 這是調用圖像

header("Content-length: ".$imgsize); 
header("Content-type: ".$imgtype); 
header("Content-Disposition: attachment; filename=".$imgname); 
print $row['img']; 

我遇到的問題是主要部分而不是圖像顯示。鏈接僅示出,所以在這種情況下,我只看到這個

http://localhost/admin/school-catalog.php?page=gallery&id=4

打開該網頁時與在URL中設置了正確的PARAMS查看圖像。


對於那些希望看到正在做保存圖像等 我複製了整段

//save image to db 
if(isset($_POST['btnupload'])) { 

$filename = $security->secure($_FILES['imgschool']['name']); 
$tmpname = $security->secure($_FILES['imgschool']['tmp_name']); 
$imgsize = $security->secure($_FILES['imgschool']['size']); 
$imgtype = $security->secure($_FILES['imgschool']['type']); 
$school = $security->secure($_POST['school']); 


//begin upload 
if($imgsize > 0) { 
$handle = fopen($tmpname, "r"); 
$content = fread($handle, filesize($tmpname)); 
$content = base64_encode($content); 
} 

$save = mysql_query("insert into tbl_schoolgallery(id,hash,img,imgtype,imgsize) values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error()); 
header("Location: school-catalog.php?page=school_gallery"); 

} 


//call image from db 
$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error()); 
while($row = mysql_fetch_assoc($query)) { 

$imgtypeget = explode("/", $row['imgtype']); 

$imgname = "img.".$imgtypeget[1]; 
$imgtype = $row['imgtype']; 
$imgsize = $row['imgsize']; 

header("Content-length: ".$imgsize); 
header("Content-type: ".$imgtype); 
print base64_decode($row['img']); 

print $row['img']; 
} 
+0

您不應該使用print $內容嗎? – 2009-06-26 05:49:59

+0

我不能因爲即時通訊從數據庫中調用圖像 – SarmenHB 2009-06-26 05:51:45

回答

7

使用addslashes是非常不正確的查詢。根據您的列是TEXT字段還是BLOB字段,您應該使用Base64或mysql_real_escape_string

使用Base64並不困難;你可以這樣使用。只需將addslashes替換爲base64_encode,然後用base64_decode回顯圖片。

有寫整個事情更容易一點的方式,對於這個問題:

// begin upload 
if ($imgsize > 0) 
{ 
    $content = file_get_content($tmpname); 
    $content = base64_encode($content); 
} 

然後輸出你真的只需要做

header("Content-type: ".$imgtype); 
echo base64_decode($img); 

如果列是一個BLOB,但是,你可以直接使用mysql_real_escape_string

// begin upload 
if ($imgsize > 0) 
{ 
    $content = file_get_content($tmpname); 
    $content = mysql_real_escape_string($content); 
} 

然後:

header("Content-type: ".$imgtype); 
echo $img; 

雖然從目前的症狀來看,我猜你也有關於如何使你的圖像被存儲並從數據庫中回憶了錯誤,而且我需要看到的那部分代碼,你在我可以幫助您修復該部分之前,使查詢插入並從數據庫中讀取。


你目前的代碼看起來大多沒問題。幾個問題:

print base64_decode($row['img']); 

print $row['img']; 

你可能是想擺脫第二行。此外,您應該使用echo而不是print;每個人都使用它,它可以更快地略低有時,和print並沒有真正有比返回值以外的任何好處:

echo base64_decode($row['img']); 

$security->secure()似乎是某種淨化的功能。只需使用mysql_real_escape_string() - 這是你應該使用的那個。除$imgsize;因爲你知道它應該是一個整數,所以你可能想在那個上使用intval()

另外這裏:

$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error()); 

您命名錶上面幾行。我假設$tbl == 'tbl_schoolgallery',但爲了保持一致性,您應該在兩個地方使用$tbl或在兩個地方使用。

此外,用if替換while - 無論如何,如果您的代碼循環多次,將會導致麻煩。