0

In my application, I store the pictures as blob in the mysql db. Now I want to display the pictures in my web application.

Now the Problem is: The images are not displayed. Just a small sign. I'm not getting any error message.

I don't know how to update my project, to display the pictures

Model function:

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; } 

Controller Method:

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>'; } ?> 
5
  • Never store pictures in database...hands off Commented Mar 21, 2017 at 11:26
  • @bub read here about storing images in databases: Storing images in a database versus a filesystem ^^ Commented Mar 21, 2017 at 11:29
  • 1
    Possible duplicate of How to display an BLOB image stored in MySql database? Commented Mar 21, 2017 at 11:35
  • I can't use this question! From my point of view, my html is almost the same as that, from the other question Commented Mar 21, 2017 at 12:52
  • I see too much code here. You need to do some basic debugging yourself. For instance, are the pictures correctly stored in DB? If you, the insert code is irrelevant to the question. And so on. (BTW, what do you want to accomplish with addslahes()?) Commented Mar 22, 2017 at 8:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.