我在上传图片时遇到了一些问题。我想从表中存储和检索图像,但是当我尝试获取图像时,它没有显示。只有损坏的图像图标。但是,如果我通过phpmyadmin手动上传图像,它将显示该图像。我的表格行的类型是blob。我很困惑。谁能帮忙。
我的代码很简单:
$pdo = new PDO('mysql:dbname=database_name;host=localhost', 'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
$stmt = $pdo->prepare('INSERT INTO image (name,image) VALUES (:name,:image)');
$stmt->execute(array('name' => $imageName, 'image' => $imageData));
echo "Image Uploaded";
$res = mysql_query("SELECT * FROM image ");
while ($row = mysql_fetch_assoc($res))
{
echo "<img src=data:image/jpeg;base64," . (base64_encode(($row['image']))) . " style='width:60px;height:60px;'>";
}
最佳答案
尝试删除验证,您使用准备好的语句,不需要它。
$pdo = new PDO('mysql:dbname=database_name;host=localhost', 'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$imageName = $_FILES["image"]["name"];
$imageData = file_get_contents($_FILES["image"]["tmp_name"]);
$imageType = $_FILES["image"]["type"];
$stmt = $pdo->prepare('INSERT INTO image (name,image) VALUES (:name,:image)');
$stmt->execute(array('name' => $imageName, 'image' => $imageData));
echo "Image Uploaded";
$res = mysql_query("SELECT * FROM image ");
while ($row = mysql_fetch_assoc($res))
{
echo "<img src=data:image/jpeg;base64," . (base64_encode(($row['image']))) . " style='width:60px;height:60px;'>";
}
关于php - 使用PDO上传图片的麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23168296/