2015-07-12 25 views
2

我目前正在嘗試創建一個.php頁面來上傳圖像以存儲在phpmyadmin的表中。然後顯示它們。在mysql/phpmyadmin中上傳和顯示圖像

首先,將圖像添加到數據庫中的表中。 phpmyadmin中的層次結構是:localhost - > images - > Greeting_Cards。

目前我正試圖插入到賀卡表中,因爲稍後我將爲多個類別創建多個表並分別顯示它們。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" 
 

 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
<title>upload</title> 
 
</head> 
 

 
<body> 
 

 
<form action="upload.php" method="post" enctype="multipart/form-data"> 
 
<input type="file" name="file_upload" /> 
 
<input type="submit" name="submit" value="upload" /> 
 
</form> 
 

 

 
<?php 
 
include '/var/db_file.php'; 
 
if(isset($_POST['submit'])) 
 
{ 
 
    $conn = mysql_connect("localhost","root", $pass); 
 
    mysql_select_db("images"); 
 
    
 
    /* Variable inits */ 
 
    $imageName = $imageData = $imageType = null; 
 

 
    $imageName = mysql_real_escape_string($_FILES["image"]["name"]); 
 
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"])); 
 
    $imageType = mysql_real_escape_string($_FILES["image"]["type"]); 
 

 
    if(substr($imageType,0,5) == "image"){ 
 
    mysql_query("INSERT INTO 'Greeting_Cards' VALUES('','$imageName','$imageData')"); 
 
    echo "Image Uploaded!"; 
 
    } 
 
    else{ 
 
    echo "Has to be an image!"; 
 
    } 
 
} 
 
?> 
 
</body> 
 
</html>

表中上傳的圖片不會出現。我正確登錄到數據庫。 「賀卡」表下的結構爲:id(int11),name(varchar30)和image(blob)。唯一的錯誤/警告顯示了在www.mydomainname.net/upload.php是:

說明:未定義指數:圖像中/var/www/html/upload.php第27行

注意:未定義指數:在/var/www/html/upload.php圖像上線28

警告:file_get_contents()函數:文件名不能在 /var/www/html/upload.php是空的線路28上

注意:未定義的索引:第29行的/var/www/html/upload.php中的圖像 已經成爲一個形象!

要顯示圖像,我必須先通過這一步驟。如果發生任何更新,將會回覆此帖。

修復1:錯誤的名稱:更改爲name =「image」以與php變量參數匹配。修復2:表格名稱後面的勾號不是單引號。可選:指定列。

在此先感謝您的幫助!

+1

輸入名稱是file_upload''input type =「file」name =「file_upload」/>'所以用作$ _FILES [「file_upload」]'而不是'$ _FILES [「image」]' –

+0

哦我的天哪......我怎麼忽視這一點。謝謝!警告和通知已消失。但是你碰巧知道我在哪裏可以找到表格中的圖像。它不在phpmyadmin中的「瀏覽」下。也許它是一個我沒有在phpmyadmin中打勾的設置? –

+0

你的查詢是錯誤的。您需要指定要插入的列。 –

回答

0

修復1:錯誤名稱:更改爲名稱= 「圖像」,以配合PHP變量參數。

修復2:返回刻度不是單引號表名稱。可選:指定列。

0

上傳圖像

<!--?php 
include("mysqlconnect.php"); 

    function GetImageExtension($imagetype) 
    { 
     if(empty($imagetype)) return false; 
     switch($imagetype) 
     { 
      case 'image/bmp': return '.bmp'; 
      case 'image/gif': return '.gif'; 
      case 'image/jpeg': return '.jpg'; 
      case 'image/png': return '.png'; 
      default: return false; 
     } 
    } 



if (!empty($_FILES["uploadedimage"]["name"])) { 

    $file_name=$_FILES["uploadedimage"]["name"]; 
    $temp_name=$_FILES["uploadedimage"]["tmp_name"]; 
    $imgtype=$_FILES["uploadedimage"]["type"]; 
    $ext= GetImageExtension($imgtype); 
    $imagename=date("d-m-Y")."-".time().$ext; 
    $target_path = "images/".$imagename; 


if(move_uploaded_file($temp_name, $target_path)) { 

    $query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES 

('".$target_path."','".date("Y-m-d")."')"; 
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 

}else{ 

    exit("Error While uploading image on the server"); 
} 

} 

?>; 

顯示影像

<!--?php 
include("mysqlconnect.php"); 

$select_query = "SELECT 'images_path' FROM 'images_tbl' ORDER by 'images_id' DESC"; 
$sql = mysql_query($select_query) or die(mysql_error());  
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){ 

?--> 

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellpadding="5" cellspacing="5"> 
<tbody><tr> 
<td> 

<img src="<?php echo $row[" images_path"];="" ?="">" alt="" />"> 

</td> 
</tr> 
</tbody></table> 

<!--?php 
} 
?-->