第一次使用堆栈溢出。
我已按照以下 2 部分的 YouTube 教程在 MYSQL 数据库中上传/存储图像。我已按照说明进行操作,但我的图像没有出现。我使用 connect.php 连接到数据库,这似乎工作正常。问题似乎出在 get.php 上,因为当我测试回显其中的任何图像时,我总是得不到图像。 使用 phpmyadmin 创建数据库并使用 xampp。
这是 YouTube 教程的链接
http://www.youtube.com/watch?v=CxY3FR9doHI
http://www.youtube.com/watch?v=vFZfJZ_WNC4&feature=fvwrel
其中包含文件
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
</form>
<?php
include 'connect.php';
//file properties
$file = $_FILES['image']['tmp_name'];
if(!isset($file))
echo "Please select an image.";
else{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name=addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE)
echo "That's not an image.";
else{
if(!$insert = mysql_query("INSERT INTO store VALUES('','$image_name','$image')"))
echo"Problem uploading image";
else{
$lastid = mysql_insert_id();
echo "image uploaded.<p />your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
</body>
</html>
这是 get.php
<?php
include 'connect.php';
$id=stripslashes($_REQUEST('id'));
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image=$image('image');
header("content-type: image/jpeg");
?>
最后连接
<?php
// connect to database
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="test";
@mysql_connect("$db_host","$db_username","$db_pass") or die("Could not connect to mysql");
mysql_select_db("$db_name")or die("Cant find database");
?>
最佳答案
您的get.php
不会回显$image
。
另外 $image=$image('image');
应该是 $image=$image['image'];
和 $_REQUEST('id' )
应为 $_REQUEST['id']
。
附注不要使用 addslashes
来防止 SQL 注入(inject)。使用mysql_real_escape_string
.
关于php - 使用 PHP 从数据库获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154664/