[英]correct way to upload image to database
我知道你們中有些人會說這不是正確的方法,但是即時通訊在緊迫的期限內完成了一個應用程序,到目前為止,我無法返回並修改代碼以將圖像存儲在目錄中。
現在那已經清除
我的問題是我通過輸入此圖像將圖像插入數據庫。
(不要介意類安全性調用,只需要檢查一下數據是否有效即可)
$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
}
變量$ content是圖像,其所有獲取內容都是加號。 我記得有人曾經提到要使用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中設置的正確參數查看圖像時。
對於那些想查看正在執行的保存圖像的查詢,等等,我復制了整個部分
//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'];
}
使用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_schoolgallery
命名tbl_schoolgallery
上方幾行。 我假設$tbl == 'tbl_schoolgallery'
,但是為了保持一致,您應該在兩個地方都使用$tbl
或在兩個地方都使用tbl_schoolgallery
。
此外,更換while
與if
-你的代碼會造成麻煩,如果它曾經循環不止一次,反正。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.