2015-03-08 37 views
0

所以我想我永遠也不會得到答案哈哈...無法顯示圖像,因爲它包含錯誤警告

老問題,請閱讀編輯

我想顯示圖片上傳到一個MySQL數據庫後。

但是通過嘗試打開圖片火狐顯示以下警告:

無法顯示圖像,因爲它包含錯誤

我已經被好幾個小時,現在搜索,因爲它的谷歌但我真的無法找到解決這個問題。所以,現在你們是我非常最後的希望:)

這裏是PHP:

<?php 
session_start(); 

$con = mysql_connect("localhost", "...", "..."); 
mysql_select_db('...',$con); 

$id = $_GET['id']; 
$result = mysql_query("SELECT image, mime FROM artikel WHERE id='$id'"); 

$row = mysql_fetch_object($result); 
header("Content-type: $row->mime"); 
echo $row->image; 
    ?> 

編輯 我認爲錯誤是在insert.php .. 可能有人請看看在它並告訴我可能會有什麼錯誤?謝謝:)(我認爲圖像不正確上傳)

<?php 
session_start(); 
header('content-type: text/html; charset=utf-8'); 
$uname = $_POST['username']; 
$typ = $_POST['typ']; 
$titel = $_POST['titel']; 
$content = $_POST['content']; 
$timestamp = time(); 

$db = new mysqli("localhost", "...", "...", "..."); 
if($db->connect_errno > 0){ 
    die("Unable to connect to database: " . $db->connect_error); 
} 

if(is_uploaded_file($_FILES['image']['tmp_name'])) { 
     // Verweis auf Bild 
     $image = $_FILES['image']['tmp_name']; 

     // Vorbereiten für den Upload in DB 
     $data = addslashes(file_get_contents($image)); 

     // Metadaten auslesen 
     $meta = getimagesize($image); 
     $mime = $meta['mime']; 
} 

//create a prepared statement 
$stmt = $db->set_charset("utf8"); 
$stmt = $db->prepare("INSERT INTO artikel (`red`, `typ`, `titel`, `content`, `image`, `mime`, `timestamp`) VALUES (?,?,?,?,?,?,?)"); 

//bind the username and message 
$stmt->bind_param('sssssss', $uname, $typ, $titel, $content, $data, $mime, $timestamp); 

//run the query to insert the row 
$stmt->execute(); 

header("Location: erfolg.php"); 

?> 

enter image description here

enter image description here

+0

您是否嘗試在註釋中設置'header'?這樣你會看到腳本拋出的任何警告/錯誤。在將腳本強制爲圖像之前,請確保沒有警告和錯誤 – DarkBee 2015-03-08 19:23:18

+0

您確定從中獲取圖像的表稱爲'artikel'嗎? – l0lander 2015-03-08 19:23:36

+0

@ l0lander是的,我是:)這是文章的德語含義。 – 2015-03-08 19:24:30

回答

0

我跟着從here指令。我用額外的MIME字段插入圖像,並檢查代碼正在工作。

確認您的mime類型是否正確。如果是的話,那麼圖像可能會被破壞。

具有簡單

header("Content-type: "); 

也爲我工作。 ScreenShot of my tableScreenshot of the data

$result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); 
     $row = mysql_fetch_object($result); 



/*Only for testing purpose.*/ 
       // echo $row->mime; //This line should output image/jpeg 
/*Only for testing purpose*/ 


/*Only to check output*/ 
     header("Content-type: $row->mime"); 

     echo $row->image; 
/*Only to check output*/ 
+0

這是我的工作文件。您可以下載它並在本地服務器上測試。 http://iseedtechnologies.in/other/downloads/stackoverflowImageEnquiry.tar.gz – 2015-03-09 19:15:16

+0

你好...現在我想我的loadpic.php工作正常 - 錯誤似乎是在插入腳本。你能看看insert.php並告訴我那裏可能是錯的嗎?謝謝:)(我添加insert.php的問題) – 2015-03-12 16:10:17

+0

你傳遞7參數,而在綁定你有8個參數。你的第5個參數是圖像,而你傳遞的是第5個參數的內容。你可以看看它是否你是對的? – 2015-03-12 17:40:04

0

試試這個。檢查您是否獲取插入值。如果插入並仍然出現錯誤,請發佈與我以前的帖子相比較的數據庫照片。

//create a prepared statement 
$stmt = $db->set_charset("utf8"); 
$stmt = $db->prepare("INSERT INTO artikel (`red`, `typ`, `titel`, `content`, `image`, `mime`, `timestamp`) VALUES (?,?,?,?,?,?,?)"); 

//run the query to insert the row 
$stmt->execute(array($uname, $typ, $titel, $content, $data, $mime, $timestamp)); 

header("Location: erfolg.php"); 
+0

好吧,看看這個問題,我添加了數據庫的屏幕截圖。順便說一句,插入$數據的圖像是正確的?或者我應該插入$ meta或$ image? – 2015-03-12 19:41:24

+0

您正在爲圖像內容分配$數據。所以你只需要爲圖片插入$數據。 $ meta是圖片大小,$ image只是名稱的參考。從您上次更新時,我只看到BLOB大小爲5B。而我的輸出給你5KiB。所以很明顯,你沒有上傳圖像內容。請處理我最新的代碼並更新。 – 2015-03-12 19:46:52

+0

好的,我更新了它。但圖像仍然無法顯示。 – 2015-03-12 19:51:43

相關問題