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>';
        }
        ?>
tereško
  • 58,060
  • 25
  • 98
  • 150
jack.k
  • 73
  • 1
  • 8
  • Never store pictures in database...hands off – B001ᛦ Mar 21 '17 at 11:26
  • @bub read here about storing images in databases: [Storing images in a database versus a filesystem](http://stackoverflow.com/questions/25196910/storing-images-in-a-database-versus-a-filesystem) ^^ – node_modules Mar 21 '17 at 11:29
  • 1
    Possible duplicate of [How to display an BLOB image stored in MySql database?](http://stackoverflow.com/questions/13214602/how-to-display-an-blob-image-stored-in-mysql-database) – Prasanth Mar 21 '17 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 – jack.k Mar 21 '17 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()`?) – Álvaro González Mar 22 '17 at 08:43

0 Answers0