您好我正试图从数据库接收我的图片。我已经可以插入图像,但我不知道它是否出错或者我在拍摄图像时出错了。
插入图片的代码:
public function Save(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
{
/*** get the image info. ***/
$size = getimagesize($_FILES['userfile']['tmp_name']);
/*** assign our variables ***/
$type = $size['mime'];
$imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['userfile']['name'];
$maxsize = 99999999;
/*** check the file is less than the maximum file size ***/
if($_FILES['userfile']['size'] < $maxsize )
{
/*** connect to db ***/
$db = new Db();
/*** our sql query ***/
$sql = "INSERT INTO Foto (image_type ,image, image_size, image_name)
VALUES ('". $type ."',
'". $imgfp ."',
'". $size ."',
'". $name ."');";
$db->conn->query($sql);
$sql = "SELECT * FROM Foto ORDER BY id DESC LIMIT 1;";
$select = $db->conn->query($sql);
$numberofRows = $select->num_rows;
if($numberofRows == 1)
{
while ($oneSelect = $select->fetch_assoc())
{
return $oneSelect;
}
} else {
throw new Exception("Fout");
}
}
else
{
throw new Exception("Bestand is te groot");
}
}
else
{
throw new Exception("Dit extensie is niet ondersteund");
}
}
加载图片的代码:
<?php
include_once("classes/Db.class.php");
//$id = $_GET['id'];
$id = "33";
$db = new Db();
$sql = "SELECT image, image_type FROM Foto WHERE id = '". $id ."';";
$result = $db->conn->query($sql);
$row = $result->fetch_assoc();
if(sizeof($row) == 2)
{
header("Content-type: ".$row['image_type']);
echo $row['image'];
}
else
{
throw new Exception("Out of bounds Error");
}
&GT;