本文介绍了pdo直接将图像插入数据库-始终插入BLOB-0B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像直接插入mysql数据库表中.在我的数据库中,我总是得到[BLOB-0B].它不会将图像插入表中.我也没有得到任何错误.我很困惑.

PHP

ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);

    include('config.php');
     if (isset($_FILES['image']) && $_FILES['image']['size'] > 0)
      {
          $tmpName  = $_FILES['image']['tmp_name'];

          $fp = fopen($tmpName, 'r');
          $data = fread($fp, filesize($tmpName));
          $data = addslashes($data);
          fclose($fp);
      }

      try
        {
            $stmt = $conn->prepare("INSERT INTO images ( picture ) VALUES ( '$data' )");
//          $stmt->bindParam(1, $data, PDO::PARAM_LOB);
            $conn->errorInfo();
            $stmt->execute();
        }
        catch(PDOException $e)
        {
            'Error : ' .$e->getMessage();
        }

HTML

<form action="upload.php" method="post">
<input id="image" name="image" type="file" />
<input type="submit" value="Upload" />
</form>
解决方案

几乎可以理解,您希望PDO::PARAM_LOB成为您在上面创建的文件指针,而不是读取fp的结果

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0)
{
   $tmpName  = $_FILES['image']['tmp_name'];

   $fp = fopen($tmpName, 'rb'); // read binary
}

try
{
   $stmt = $conn->prepare("INSERT INTO images ( picture ) VALUES ( ? )");
   $stmt->bindParam(1, $fp, PDO::PARAM_LOB);
   $conn->errorInfo();
   $stmt->execute();
}
catch(PDOException $e)
{
   'Error : ' .$e->getMessage();
}

I'm trying to insert the image into mysql database table directly. In my database I'm always getting [BLOB - 0B]. it doesn't insert images into table. I didn't get any error too. I'm confused..

PHP

ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);

    include('config.php');
     if (isset($_FILES['image']) && $_FILES['image']['size'] > 0)
      {
          $tmpName  = $_FILES['image']['tmp_name'];

          $fp = fopen($tmpName, 'r');
          $data = fread($fp, filesize($tmpName));
          $data = addslashes($data);
          fclose($fp);
      }

      try
        {
            $stmt = $conn->prepare("INSERT INTO images ( picture ) VALUES ( '$data' )");
//          $stmt->bindParam(1, $data, PDO::PARAM_LOB);
            $conn->errorInfo();
            $stmt->execute();
        }
        catch(PDOException $e)
        {
            'Error : ' .$e->getMessage();
        }

HTML

<form action="upload.php" method="post">
<input id="image" name="image" type="file" />
<input type="submit" value="Upload" />
</form>
解决方案

You almost got it, you want PDO::PARAM_LOB to be a file pointer which you created above, not the result of reading the fp

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0)
{
   $tmpName  = $_FILES['image']['tmp_name'];

   $fp = fopen($tmpName, 'rb'); // read binary
}

try
{
   $stmt = $conn->prepare("INSERT INTO images ( picture ) VALUES ( ? )");
   $stmt->bindParam(1, $fp, PDO::PARAM_LOB);
   $conn->errorInfo();
   $stmt->execute();
}
catch(PDOException $e)
{
   'Error : ' .$e->getMessage();
}

这篇关于pdo直接将图像插入数据库-始终插入BLOB-0B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:22
查看更多