本文介绍了pdo 直接将图像插入数据库 - 始终插入 BLOB - 0B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
限时送ChatGPT账号..
我正在尝试将图像直接插入到 mysql 数据库表中.在我的数据库中,我总是得到 [BLOB - 0B].它不会将图像插入表格中.我也没有收到任何错误.我糊涂了..
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>
推荐答案
你差不多明白了,你想让 PDO::PARAM_LOB
成为你上面创建的文件指针,而不是读取的结果fp
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!