本文介紹了pdo 直接將圖像插入數據庫 - 始終插入 BLOB - 0B的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試將圖像直接插入到 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!