2012-08-08 83 views
1

我在從數據庫中檢索圖像時遇到問題。下面是上傳圖像到數據庫的代碼:使用mysql在php中檢索並上傳圖像

<form method="post" action="index2.php" enctype="multipart/form-data"> 
    <input type="file" name="drimg2"/> 
    <input type="submit" name="submit2" value="Save"/> 
</form> 

<?php 

if(isset($_POST['submit2'])) { 

    $con=mysql_connect("localhost","root","root"); 
    mysql_select_db("test",$con); 

    $imgname1=$_FILES['drimg2']['name']; 
    echo $imgname1; 

    $imageextension1 = substr(strrchr($imgname1,'.'), 1); 
    if (($imageextension1!= "jpg") && ($imageextension1 != "jpeg") && ($imageextension1 != "gif")&& ($imageextension1 != "png")&& ($imageextension1 != "bmp")) { 
    die('Unknown extension. Only jpg, jpeg, and gif files are allowed. Please hit the back button on your browser and try again.'); 
    } 

    if (($imageextension1= "jpg") && ($imageextension1= "jpeg") && ($imageextension1= "gif") && ($imageextension1 = "png") && ($imageextension1 = "bmp")) { 
    $query1=mysql_query("INSERT INTO store set image='$imgname1'"); 
    $action = move_uploaded_file($_FILES['drimg2']['tmp_name'],"images/".$imgname1); 
    die('not Uploded'); 
    } 
} 
?> 

現在我想檢索數據庫中的所有圖像;爲了這個,我現在用的是下面的代碼:

<?php 
$query1="select * from store"; 
$fetch=mysql_query($query1); 

while ($rows=mysql_fetch_array($fetch)) { 
    echo "<img src='images/".$rows['image']."' />"; 
} 
?> 
+0

TBH你不能上傳圖像只有'「;}?>' – EaterOfCode 2012-08-08 07:49:58

+0

檢查我的代碼user2727841 – user2727841 2014-01-01 06:53:28

回答

0

請參閱本link

希望這會有所幫助。圖像的代碼下面 更改檢索,

$content = $row['image']; 

header('Content-type: image/jpg'); 
    echo $content; 
+1

嘗試發佈一個例子,以防萬一該網站下線 – JConstantine 2012-08-08 07:57:24

+0

$查詢=」 SELECT * FROM賣場裏的id = 30 「; $ result = mysql_query($ query); while($ row = mysql_fetch_array($ result)){echo $ img = $ row ['name']; echo」「} 我使用此代碼來顯示圖像使用它的路徑 – 2012-08-11 07:59:00

+0

@WajahatAhmed你想要做什麼?你想從數據庫服務器還是從文件服務器中檢索你存儲的圖像?在你的數據庫中,似乎你想從數據庫中獲得它,現在你正在從文件中獲取它服務器。具體,所以我可以幫你。 – Vinay 2012-08-13 05:08:40

1

你不應該使用舊的mysql_ *功能和使用PDO或mysqli的替代。這是一種更乾淨,更安全的做你想做的事情的方式。

<?php 
/** 
* A Simple class to handle your database requests 
* related to your image storage ect 
*/ 
class image_model{ 
    private $db; 
    function __construct($db){ 
     $this->db = $db; 
    } 

    function add($img_name){ 
     $sql = "INSERT INTO store (image) VALUES (:value)"; 
     $stmt = $this->db->prepare($sql); 
     $stmt->bindParam(':value', $img_name, PDO::PARAM_STR); 
     $stmt->execute(); 
    } 

    function get_all(){ 
     $sql = "SELECT image FROM store"; 
     return $this->db->query($sql)->fetchAll(); 
    } 
    //Perhaps use in future 
    function get_image($id){ 
     $sql = "SELECT image FROM store WHERE id=:id"; 
     $stmt = $this->db->prepare($sql); 
     $stmt->bindParam(':id', $id, PDO::PARAM_INT); 
     $stmt->execute(); 
     return $result->fetchAll(); 
    } 
} 

//Connect safely to your database... 
try{ 
    $db = new PDO("mysql:host=localhost;dbname=test", 'root', 'password'); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); 
}catch (Exception $e){ 
    die('Cannot connect to mySQL server. Details:'.$e->getMessage()); 
} 

//Create an instance of the image model above 
$img_model = new image_model($db); 

//Boom...Handle the upload 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
    if ($_FILES["img"]["error"] > 0){ 
     echo "Error: ".$_FILES["img"]["error"]."<br />"; 
    }else{ 
     $img = getimagesize($_FILES["img"]["tmp_name"]); 

     $allowed = array('image/jpeg','image/gif','image/png','image/bmp'); 
     //Width and height must be more then 0 pixles and mime must be in allowed array 
     if($img[0] > 0 && $img[1] > 0 && in_array($img['mime'],$allowed)){ 
      if(is_uploaded_file($_FILES["img"]["tmp_name"])){ 
       //Clean image name 
       $img_name = preg_replace('/[^a-zA-Z0-9.-]/s', '_', basename($_FILES["img"]["name"])); 
       //move image to folder 
       move_uploaded_file($_FILES["img"]["tmp_name"],"images/".$img_name); 
       //Add image to db using a method from the image model 
       $img_model->add($img_name); 
      } 
     }else{ 
      echo "Error: Unknown extension. Only jpg, bmp and gif files are allowed. Please hit the back button on your browser and try again.<br />"; 
     } 

    } 
} 

//Your form 
?> 
<h1>Upload image</h1> 
<form method="POST" action="" enctype="multipart/form-data"> 
<input type="file" name="img"/> 
<input type="submit" name="submit" value="Save"/> 
</form> 

<?php 
//Access your image model in a simple way and get all images 
foreach($img_model->get_all() as $row){ 
    echo '<img src="images/'.$row['image'].'" /><br />'; 
} 
?> 
+0

它沒有顯示任何內容 – 2012-08-08 10:46:54

+0

我認爲wajahat ahmed是tr ying學習文件上傳,你告訴他哎呀編程! – user2727841 2014-01-01 06:55:06

+0

@ user2727841,什麼是oops編程?是不是當你認爲你做錯了,讓你覺得哎呀? lulz – 2014-01-01 14:35:37

-1

試試這個我測試了這段代碼並做了一些修改。

<form method="post" action="index2.php" enctype="multipart/form-data"> 
    <input type="file" name="drimg2"/> 
    <input type="submit" name="submit2" value="Save"/> 
</form> 

<?php 
if(isset($_POST['submit2'])) { 
    $con=mysql_connect("localhost","root","root"); 
    mysql_select_db("test",$con); 
    $imgname1=$_FILES['drimg2']['name']; 
    //echo $imgname1; 
    $imageextension1 = substr(strrchr($imgname1,'.'), 1); 
    //echo '<pre>';print_r($imageextension1);echo '</pre>';die('Call'); 
    if (($imageextension1!= "jpg") && ($imageextension1 != "jpeg") && ($imageextension1 != "gif")&& ($imageextension1 != "png")&& ($imageextension1 != "bmp")) { 
    die('Unknown extension. Only jpg, jpeg, and gif files are allowed. Please hit the back button on your browser and try again.'); 
    } else { 
    //if (($imageextension1 == "jpg") && ($imageextension1 == "jpeg") && ($imageextension1 == "gif") && ($imageextension1 == "png") && ($imageextension1 = "bmp")) { 
     if(move_uploaded_file($_FILES['drimg2']['tmp_name'],"images/".$imgname1)) { 
      $query1=mysql_query("INSERT INTO fileurl(filename) VALUES('".$imgname1."')"); //fileurl is table name and filename is field name 
      if($query1) { 
       echo "Data inserted"; 
      } else { 
       echo "Data not inserted"; 
      } 
     } else { 
      echo "Error occured while trying to upload image"; 
     } 
    //$action = ; 
    //die('not Uploded'); 
    } 
} 
?> 

和獲取圖像試試這個

$query = "SELECT * FROM fileurl"; 
$fetch = mysql_query($query); 
while($rows = mysql_fetch_array($fetch)) { 
    echo "<img src='images/".$rows['filename']."' />"; 
} 

如果你的圖像格式不匹配,它將關閉程序自動,否則它會跳到else條件和上傳的圖片,然後插入名的文件到數據庫中。