我在上传图片方面遇到了一些问题。我想从表中存储和检索图像,但是当我尝试获取图像时,它并没有显示。只有破碎的图像图标。但是,如果我通过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;'>";
}
答案 0 :(得分:1)
尝试删除验证,使用预准备语句,不需要它。
$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;'>";
}