2013-02-17 43 views
-2

我试图使用php和mysqli将图像上传到数据库中作为blob。如果我只是尝试使用查询来插入图像名称,它会工作得很好,并创建一个名称为新的行(blob为空)。一旦我开始尝试使用查询和jQuery函数通过表单将数据上传到数据库中,就会创建一个新行,但该数据块显示0 B。代码的第一位是html表单。第二个是提交表单后调用的php文件。任何建议,将不胜感激。提前致谢。PHP和mysqli:使用表格将blob上传到数据库

澄清:据我所知,上传图像到目录是一种更有效的方式来存储它。为了理解如何使用其他选项,我试图弄清楚如何使用blob工作。

编辑

只是为了澄清我也试过下面的代码片段。

if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) { 
     echo "Import of photo success"; 

     $aimage = file_get_contents($tmpfile); 

     if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) { 
      echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error; 
     } 

     if (!$stmt->bind_param("sb", $_POST['aname'], $aimage)) { 
      echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error; 
     } 

     if (!$stmt->execute()) { 
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
     } 

     else { 
      printf("%d row inserted.<br/>", $stmt->affected_rows); 
     } 
    } 

编辑完

错误消息导入照片的成功

注意:未定义指数:aimage中/ NFS/STAK /学生/ M/martalic /的public_html/CS494 /第43行的Test/addActorInfo.php

警告:file_get_contents()[function.file-get-contents]:文件名不能在/ nfs/stak/stude中为空NTS/M/martalic /的public_html/CS494 /测试/ addActorInfo.php上线43 1行插入

HTML表单

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
<form id="form" method="post" action=addActorInfo.php enctype="multipart/form-data"> 
    Actor Name: <input id="aname" type="text" name="aname" class="required" maxlength="64"><br><br> 
    Attach Photo: <input id="aimage" type="file" name="aimage" class="required"><br><br> 
    <input type="submit" name="ADD" value="ADD"/> 
</form> 

</body> 
</html> 

PHP

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
Actor Information 
<?php 
ini_set('display_errors', 'On'); 
ini_set('display_startup_errors', 'On'); 
error_reporting(E_ALL); 

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 

if(isset($_POST["ADD"])) { 
    $aname = $_POST['aname']; 
    $errorinfo = $_FILES["aimage"]["error"]; 
    $filename = $_FILES["aimage"]["name"]; 
    $tmpfile = $_FILES["aimage"]["tmp_name"]; 
    $filesize = $_FILES["aimage"]["size"]; 
    $filetype = $_FILES["aimage"]["type"]; 

    if (!($filetype == "image/jpeg" && $filesize > 0)) { 
     echo "Import of photo failed"; 
    } 

    if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) { 
     echo "Import of photo success"; 

     if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage) VALUE (?,?)"))) { 
      echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error; 
     } 
     $null = NULL; 
     if (!$stmt->bind_param("sb", $_POST['aname'], $null)) { 
      echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error; 
     } 

     if (!$stmt->send_long_data(0, file_get_contents($_POST['aimage']))) { 
      echo "Did not get contents"; 
     } 

     if (!$stmt->execute()) { 
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
     } 

     else { 
      printf("%d row inserted.<br/>", $stmt->affected_rows); 
     } 
    } 
    else { 
     echo "Image must be under 1 MB"; 
    } 
    $stmt->close(); 
} 
$mysqli->close(); 
?> 
</body> 
</html> 

回答

0

你有没有考虑像你一样上传图像,然后把它放在一个文件夹/ uploadedimg /然后引用文件名(随机生成)并将其插入到数据库? BLOB效率不高。

所以上传。名称文件。保存存档。在DB查询文件imgName = myfile123.png,然后当你需要它只是使用

$myvar = $SQLQuery; 

<img src="/uploadedimg/". $myvar ." " alt="my image" /> 
+0

绝对。我正在尝试使用blob来获取它的工作方式。 – user1852050 2013-02-17 08:07:53

+0

不要忘记BLOB的东西,看起来你的代码是正确的上传,只是注意你上传它的位置,并将文件的名称作为VARCHAR放入数据库。不要让BLOB让你失望。 – JonE 2013-02-17 08:24:07

+0

感谢您的建议,但我有兴趣了解如何blob工作上传和存储。我可能会也可能不会再使用它,但至少我会理解它的优点和缺点,因为我已经尝试过了。 – user1852050 2013-02-17 08:28:04

0

在我的绑定参数发现问题。通过从“b”切换到“s”,我能够将图像作为blob存储在我的数据库中。