我正在尝试显示存储在 mysql 数据库中的图像。我这样存储它:
if (isset($_SESSION['mod']) && (isset($_GET['upload'])) ) {
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$con = mysql_connect("localhost", "root");
mysql_select_db("psi", $con);
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
//now i use <tmpName> as an actual name of file
$tmpName = $_FILES['image']['name'];
if (isset($_GET['name']))
$tmpName = $_GET['name'];
$uname = $_SESSION['uname'];
$idObj = mysql_query("SELECT id_object AS id FROM tobject WHERE uname = '$uname'");
$idObj = mysql_fetch_assoc($idObj);
$idObj = $idObj['id'];
// Create the query and insert
// into our database.
$query = "INSERT INTO slike ";
$query .= "VALUES ('', '$idObj', '$data', '$tmpName')";
$results = mysql_query($query, $con);
// Print results
print "Thank you, your file has been uploaded.";
}
else {
print "No image selected/uploaded";
}
}
我想这没关系..它确实在db中存储了一些东西(适当的大小),但我看不到它是手动的..所以,当我尝试用这段代码获取它时:
else if (isset($_GET['idSlike'])) {
$idSlike = $_GET['idSlike'];
$con = mysql_connect("localhost", "root");
mysql_select_db("psi", $con);
$res = mysql_query("SELECT slika FROM slike WHERE id_slika = '$idSlike'");
if (!$res) {
die("greska: " . mysql_error());
};
$slika = mysql_fetch_array($res);
$slika = $slika['slika'];
header('Content-Type: ' . $slika['mimetype']);
echo $slika;
}
注意:从数据库存储和获取图像都在同一个文件(image.php)中......
我什么也没得到...我尝试用以下方式显示它:
<img src="image.php?idSlike=10"/>
我对 id 进行了硬编码,但它们存在于 db 中
我也试过
echo "<img src=\"image.php?idSlike=13\">";
通过另一个 php 文件,但我得到的只是一个空图像(带有正确的 src)
我正在使用 xampp (mysql 5.5.16; PHP 5.3.8) ...