2012-09-19 91 views
-4

我已經上傳圖像數據庫,但是當我想顯示它的圖像不能顯示..我不知道它爲什麼不能顯示..可能在我的編碼有問題..你可以幫助我??圖片無法顯示

upload.php的

<?php 

$id = $_POST['account']; 
$code = $_POST['code']; 
$price = $_POST['price']; 

echo $file = $_FILES['image']['tmp_name']; 
if (!isset($file)) 
    echo "Please select an image."; 
else 
{ 
$image = addslashes (file_get_contents($_FILES['image']['tmp_name'])); 
$image_name = addslashes ($_FILES['image']['name']); 
$image_size = getimagesize($_FILES['image']['tmp_name']); 

if($image_size==FALSE) 
    echo "That's not an image."; 
else 
{ 
if (!$insert = mysql_query("INSERT INTO menu 
    VALUES('$code','$price','$image','$id')")) 
echo "Problem uploading images."; 
else 
{ 
    $lastid = $code; 
    echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>"; 
} 

    } 
} 
?> 

get.php

<?php 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("food", $con); 


$id = addslashes($_REQUEST['FoodId']); 

$image = mysql_query("SELECT * FROM menu WHERE FoodId=$id"); 
$image = mysql_fetch_assoc($image); 
$image = $image['image']; 

header("Content-type: image/jpeg"); 

echo $image; 
?> 
+0

你可以在這裏粘貼什麼錯誤顯示? –

+0

爲什麼你將圖像內容存儲在數據庫中?保存圖像的列的類型是什麼? –

+0

BLOB DATA TYPE .. – user1683166

回答

0

請確認通過您的數據庫,該圖像已成功存儲手動尋找,並INFACT可讀?張貼存儲內容圖片。

僅供參考,通常最好是將圖像存儲在服務器上的文件夾中,而不是存儲在數據庫中,並將路徑存儲在數據庫中。

如果你想存儲的路徑圖像,像這樣的工作:

<?php 

$id = mysql_real_escape_string($_POST['account']); 
$code = mysql_real_escape_string($_POST['code']); 
$price = mysql_real_escape_string($_POST['price']); 

$allowedExts = array("jpg", "jpeg", "gif", "png"); 
if(!isset($_FILES)) 
{ 
    die('No Image Uploaded - Please check form enctype="multipart/form-data".'); 
} 
$fileinfo = pathinfo($_FILES['image']['name']); 
$extension = $fileinfo['extension']; 
if(!in_array($extension,$allowedExts)) 
{ 
    die('Invalid file type.'); 
} 
$file = '/uploadedimages/' . uniqid() .'.'. $extension; 
if(!move_uploaded_file($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . $file)) 
{ 
    die('Problem Storing Image in folder /uploadedimages/ - Please check folder exists and is writable.'); 
} 

$insert = mysql_query("INSERT INTO menu VALUES('$code','$price','$file','$id')"); 
if(!$insert) 
{ 
    die('Problem adding to db.'); 
} 
/* 
the following two echos demostrate two methods of showing the image, 
the first adds the variable by splitting the string and rejoining it, 
the second nests the image inside the variable without the need to edit it. 

The difference is the single and double quotes, single quotes take any content 
inside them as litteral, meaning 
    $string = "hello"; 
    $new_string = '$string Dave.'; 
    will print $string Dave. Not hello Dave. 
Double quotes will accept variables and add the content they hold. 
    $string = "hello"; 
    $new_string = "$string Dave."; 
    will print hello Dave. 
*/ 
echo 'Image Uploaded.<p>Your Image</p><img src="' . $file . '" />'; 

echo "Image Uploaded.<p>Your Image</p><img src=\"$file\" />"; 

的文件夾/ uploadedimages /和將需要寫使能(chmod777)

從另一個文件調用; 您可以隨時使用您之前使用的查詢從數據庫調用圖像;

$res = mysql_query("SELECT * FROM menu WHERE FoodId=$id"); 
$row = mysql_fetch_assoc($res); 
$image = $row['image']; 
?> 
<img src="<?=$image?>" /> 

很明顯,$ id是你已經在php頁面上定義的東西。

編輯:其他人向我發佈的文章是不必要的 - 使用帶變量的單引號並沒有什麼錯誤,它清楚地顯示了你在哪裏添加變量。如果你想展示如何使用雙引號你應該提供兩種選擇並解釋不同之處。你的編輯很俗氣,寫得很糟糕,在雙引號之後增加一個空格。

+0

我不明白什麼chmod777?並在我的數據庫中,我必須使用哪些數據類型來保存圖像? BLOB還是什麼? – user1683166

+0

你可以使用varchar(70)。 chmod777將您存儲圖像的目錄設置爲可寫,否則它將不會被寫入。您可以在FTP應用程序中執行此操作,通常通過右鍵單擊文件夾並更改權限並勾選所有選項寫入讀取執行 –

+0

我假設您在Linux網絡服務器上 - chmod僅適用於Linux,有一個相當於Windows。 –

0

你爲什麼不閉上你的img元素?

也尤爲明顯,讓你VAR出字符串的:

echo "Image uploaded.<p>Your image:</p><img src=\"get.php?id=$lastid\" />"; 
+0

它不工作... – user1683166

2

1)記住little Bobby tables;始終將輸入清理到數據庫中。

2)不要使用舊mysql_* functions,它們是不安全的,貶值(見紅色框here)。相反,看看使用PDO或MySQLi,他們不需要很長時間學習,並且在各方面都更好,包括一旦習慣它們後便於使用。

3)將圖片保存在服務器上並僅將圖片url存儲在數據庫中會好得多。這是有很多原因的;不僅如此,如果您允許用戶上傳大量圖像,那麼當您擁有超過一定數量的圖像時,您的數據庫將會有幾Gb的大小,讀取速度非常慢,而且更難定期備份。

上傳文件到您的服務器:How to upload & Save Files with Desired name

然後你只需要在文件名存儲在數據庫中。然後,您可以從數據庫中檢索文件名,並在需要顯示該圖像時將其添加到應用程序中的img標記中。

+0

我不知道編碼.. – user1683166

+0

哪位? PDO/mySQLi或PHP - 如何使用Desired name鏈接上傳和保存文件,或從數據庫中獲取URL? – Stu