在我的应用程序中,我将图片存储为mysql db中的blob。 现在我想在我的网络应用程序中显示图片。
现在的问题是: 图像不会显示。只是一个小标志。我没有收到任何错误消息。
我不知道如何更新我的项目,以显示图片
模特功能:
public function create($fileName, $fileType, $fileSize, $fileContent, $gallery){
$query = "INSERT INTO $this->tableName (name, type, size, content, gallery_ID) VALUES (?, ?, ?, ?, ?)";
$statement = ConnectionHandler::getConnection()->prepare($query);
$statement->bind_param('ssisi', $fileName, $fileType, $fileSize, $fileContent, $gallery);
$success = $statement->execute();
if (!$success) {
throw new Exception($statement->error);
}
}
public function listByID($galleryID){
$query = "SELECT * from $this->tableName where gallery_ID = ?";
$statement = ConnectionHandler::getConnection()->prepare($query);
$statement->bind_param('i', $galleryID);
$statement->execute();
$result = $statement->get_result();
if (!$result) {
throw new Exception($statement->error);
}
$rows = array();
while ($row = $result->fetch_object()) {
$rows[] = $row;
}
return $rows;
}
控制器方法:
public function doAddPhoto(){
$fileName = $_FILES['fileToUpload']['name'];
$fileSize = $_FILES['fileToUpload']['size'];
$fileType = $_FILES['fileToUpload']['type'];
$tmpName = $_FILES['fileToUpload']['tmp_name'];
$gallery = $_SESSION['gallery'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($_FILES['fileToUpload']['size'] <= 0 ){
echo '<div class="alert alert-danger" id="messsage" role="alert">No Picture selected</div>';
}
else if ($_FILES["fileToUpload"]["size"] > 4194304) {
echo '<div class="alert alert-danger" id="messsage" role="alert">File to big</div>';
}
else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo '<div class="alert alert-danger" id="messsage" role="alert">Sorry, only JPG, JPEG, PNG & GIF files are allowed.</div>';
}
else {
$fp = fopen($tmpName, 'r');
$fileContent = fread($fp, filesize($tmpName));
fclose($fp);
if(!get_magic_quotes_gpc()){
$fileName = addslashes($fileName);
}
$photoModel = new PhotoModel();
$photoModel->create($fileName, $fileType, $fileSize, $fileContent, $gallery);
}
header('location: /gallery/ListGalleriesPerUserOverview');
}
public function showPhotosPerUser(){
if (!isset($_SESSION ['loggedin']) || $_SESSION ['loggedin'] != true)
{
header('location: /');
return;
}
else{
$view = new View('gallery');
$galleryID = $_SESSION['gallery'];
$photoModel = new PhotoModel($galleryID);
$photos = $photoModel->listByID($galleryID);
$view->photos = $photos;
$view->display();
}
}
HTML:
<form action="/photo/doAddPhoto" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="uploadBtn">
</form>
<?php
foreach ($photos as $photo){
$content = $photo->content;
echo '<div class="col-md-3 portfolio-item">
<a href="#">
<img src="data:image/jpeg;base64,'. base64_encode($content) .'" />
</a>
</div>';
}
?>