我在DB中插入图像时遇到问题。 该表具有以下结构:
插入图像的PHP代码是:
if($_FILES['file']['error']==0){
$result = is_uploaded_file($_FILES['file']['tmp_name']);
if(!$result){
echo "Upload failed";
}else{
$type = explode("/", $_FILES['file']['type']);
$extension = $type[1];
$name = $_FILES['file']['name'];
$img = $_FILES['file']['tmp_name'];
$img = file_get_contents($_FILES['file']['tmp_name']);
$img = addslashes ($img);
}
$sql = "INSERT INTO images (name, extension, img) VALUES ('$name', '$extension', '$img')";
$result = $mysqli->query($sql);
if($result){
echo "insertion was successful";
}else{
echo "insertion failed: ".$mysqli->error;
}
这就是我试图看到img的方式:
$sql = "SELECT name, extension, img FROM images WHERE id='1'";
$result = $mysqli->query($sql);
if($result){
$a = $result->fetch_assoc();
header ("Content-type: image/".$a['estensione']);
echo $a['img'];
}else{
echo "AAAAAAAAA<hr>";
echo $mysqli->error;
}
插入正常,但我无法查看图像。 另外,还有另一种在Db上传图像的方法吗?
答案 0 :(得分:1)
图片标记示例中的第一个(您要显示结果的页面)
标签中的从fetch_image_frm_db.php页面获取图像,并显示
<img src="fetch_image_frm_db.php?id=<?php echo $id_of_row;?>"/>
fetch_image_frm_db.php页面
$id=$_GET['id'];
$query = "SELECT * FROM images WHERE id=$id";
$result=mysql_query($query) or die('Error, query failed'.mysql_error());
$row=mysql_fetch_array($result);
header("Content-type:image/jpeg");
stripslashes ($row['img']);
echo $row['img'];
* * 在db中存储图像不是一种好习惯
答案 1 :(得分:0)
我认为你在第5行的代码中拼错了扩展名:
$sql = "SELECT name, extension, img FROM images WHERE id='1'";
$result = $mysqli->query($sql);
if($result){
$a = $result->fetch_assoc();
header ("Content-type: image/".$a['extension']);
echo $a['img'];
}else{
echo "AAAAAAAAA<hr>";
echo $mysqli->error;
}
答案 2 :(得分:0)
好的,我这样解决了...... 索引页面的代码是:
<?php
$mysqli = new mysqli("127.0.0.1","root","root","prova");
if($mysqli->connect_errno){
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(isset($_POST['submit'])){
if(isset($_FILES['file'])){
if($_FILES['file']['error']==0){
$result = is_uploaded_file($_FILES['file']['tmp_name']);
if(!$result){
echo "Impossibile eseguire l'upload";
}else{
$type = $_FILES['file']['type'];
$nome = $_FILES['file']['name'];
$img = file_get_contents($_FILES['file']['tmp_name']);
$img = addslashes ($img);
}
$sql = "INSERT INTO immagini (nome, estensione, img) VALUES ('$nome', '$type', '$img')";
$result = $mysqli->query($sql);
if($result){
echo "Insertion was succesfully executed";
}else{
echo "Insertion failed: ".$mysqli->error;
}
}
}
}
echo "<br><br><hr>";
$query = "SELECT * FROM immagini";
$result=$mysqli->query($query) or die('Error, query failed'.mysql_error());
while($row = mysqli_fetch_array($result)){
echo "<p><img height='250' width='250' src=\"fetch_img.php?id=$row[id]\">";
}
?>
这是脚本fetch_img.php,taht有效打印图像:
<?php
$mysqli = new mysqli("127.0.0.1","root","root","prova");
if($mysqli->connect_errno){
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if($_GET['id']==""){
header("Location: index.php");
}
$id=$_GET['id'];
$query = "SELECT * FROM immagini WHERE id=$id";
$result=$mysqli->query($query) or die('Error, query failed'.mysql_error());
//var_dump($result);
$row= $result->fetch_assoc();
header("Content-type: image/jpeg");
$rs_immagine=imagecreatefromstring($row['img']);
imagejpeg( $rs_immagine, null, 100);
imagedestroy($rs_immagine);
?>
感谢所有人,同时也建议将数据库中存储文件(尤其是图像)存储在内部......我是第一个说这个的人,但我必须这样做。