如何使用PHP在MySQL数据库中插入图像?

时间:2022-01-22 07:39:24

I am using this form:

我使用这个表格:

<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>

and in test1.php

并在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/*");

and getting errors are:-

并得到错误: -

file_get_contents(DSC02945.JPG) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\thinstrokes original site\testimage1.php on line 22

file_get_contents(DSC02945.JPG)[function.file-get-contents]:无法打开流:第22行的C:\ xampp \ htdocs \ thinstrokes原始站点\ testimage1.php中没有这样的文件或目录

Warning: getimagesize(DSC02945.JPG) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\thinstrokes original site\testimage1.php on line 23

警告:getimagesize(DSC02945.JPG)[function.getimagesize]:无法打开流:第23行的C:\ xampp \ htdocs \ thinstrokes原始网站\ testimage1.php中没有此类文件或目录

how can i correct it..???

我怎么能纠正它.. ???

2 个解决方案

#1


3  

You need enctype=multipart/form-data in your form declaration. And access the file through the $_FILES variable instead of the $_POST variable. Like:

您需要在表单声明中使用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'];   
?>

#2


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


3  

You need enctype=multipart/form-data in your form declaration. And access the file through the $_FILES variable instead of the $_POST variable. Like:

您需要在表单声明中使用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'];   
?>

#2


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);