2012-05-31 184 views
1

我使用這種形式:如何使用PHP在MySQL數據庫中插入圖像?

<FORM action="testimage1.php" method="post"> 
       <div style="font:bold 10px arial,serif;" >Product Name*</div> 
       <input type="text" name="myuserName" maxlength="50" /><br /> 
        <div style="font:bold 10px arial,serif;" >Upload a photo</div> 
       <input name="uploadimage" type="file" /></br> 
       <div style="font:bold 10px arial,serif;">Product Description:</div> <input type="text" name="product" value=""></br> 
       <input id="submit" type="submit" value="submit" /><br /> 
       </form> 

和test1.php

require_once("dbconnect.inc.php"); //for database connection     
    $db_name="thinstrokes";          
    $tbl_name="product"; 
    $db_selected=mysql_select_db("$db_name")or die("cannot select DB"); 
    // Connect to server and select databse. 
    // username and password sent from form 
    $myusername=$_POST['myusername']; 
    $myproduct=$_POST['product']; 
    $filename=$_POST['uploadimage']; 

$imgData = file_get_contents($filename); 
    $size = getimagesize($filename); 
    $sql = "INSERT INTO product 
    (productname, image_id , image_type ,image, image_size, image_name,productdesc) 
    VALUES 
    ('$myusername','11', '{$size['mime']}', '{$imgData}', '{$size[3]}', 
    '{$_FILES['userfile']['name']}','$productdesc')"; 
    $result=mysql_query($sql) or die("error in uploading/*"); 

並得到錯誤是: -

的file_get_contents(DSC02945.JPG)function.file,得到-contents]:無法打開流:第22行中沒有C:\ xampp \ htdocs \ thinstrokes原始網站\ testimage1.php中的此類文件或目錄

Warning:getimagesize(DSC02945.JPG)[function.getimagesize]:無法打開流:沒有這樣的文件或目錄在C:\ xampp \ htdocs \ thinstrokes原來的網站\ testimage1.php上線23

怎麼可以我糾正它..?

+1

ENCTYPE應的multipart/form-data的 – Satya

+0

還檢查是否在'product'表'image'屬性是'BLOB'類型(否則將二進制數據可以由於字符集轉換被弄亂) –

+0

爲什麼你要插入圖像數據庫....通常我們將圖像存儲在一個文件夾中,並將圖像名稱存儲在分貝...它會讓你的分貝清潔...... –

回答

3

您的表單聲明中需要enctype = multipart/form-data。並通過$ _FILES變量而不是$ _POST變量來訪問文件。像:

<form action="testimage1.php" method="post" enctype="multipart/form-data"> 
    <input name="uploadimage" type="file" /> 
</form> 

<?php 
    $filename = $_FILES['uploadimage']['tmp_name']; 
?> 
+0

現在它給mysql查詢錯誤...? – Neeraj

+0

我糾正了這樣的代碼.. – Neeraj

+0

這是一個完整的其他問題。你現在文件上傳成功了,這是對當前問題的回答。 –

0
$imgData = file_get_contents($filename); 
$size = getimagesize($filename); 
mysql_connect("localhost", "$username", "$password"); 
mysql_select_db ("$dbname"); 
$sql = sprintf("INSERT INTO testblob 
    (image_type, image, image_size, image_name) 
    VALUES 
    ('%s', '%s', '%d', '%s')", 
    mysql_real_escape_string($size['mime']), 
    mysql_real_escape_string($imgData), 
    $size[3], 
    mysql_real_escape_string($_FILES['userfile']['name']) 
    ); 
mysql_query($sql); 
+1

請解釋你糾正的是什麼,以及錯誤是什麼。 – iblamefish

相關問題