2012-05-31 185 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

相关问题