Image from database is not being displayed!?

Sorry in advance (was not sure to put this in databases section or PHP section).

Right now I am having trouble echo’ing users avatars that they upload at the registration page. I am currently testing this on my localhost but when I try to echo the avatar it just displays the alt="" of the image instead of the actual image. Here is the code I am using to echo the image:

echo '
    <div class="navbar-right">
       <!-- icons -->
       <img src="http://i.imgur.com/HD6W7XJ.png" class="icon-search" alt="search-icon" />

       <span class="icon-bars-button">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
       </span>

    ';

    // if user doesn't have avatar set:
    if (empty($avatar)) {

      // echo default avatar...
      echo '<img src="http://i.imgur.com/QFxs0nX.png" class="user_avatar" alt="default avatar" />';

    // if user has set an avatar:
    } else {

      // echo set avatar...
      echo '
      <!-- user avatar -->
      <img src="data:image/jpeg;base64,' . base64_encode($avatar) . '" class="user_avatar" alt="' .$username. ' avatar" />
      ';

    }

    echo '
    </div>';

… and here is what it looks like on the navbar where it should be displayed:
https://puu.sh/sqxyX/1b1e03bcd4.png


As you can see the image is not being displayed but the image is being inserted into my database and is in the row. If you need more code here is some other code that works with the $avatar variable:
register.php
function showHeader()
function checkLogin()

are you sure your images are always jpeg as you wrote in data:image? what does the generated sourcecode looks like?

The generated source code is as followed (when I echo $avatar;):
13103372_582930875200801_5507892662037077660_n.jpg

How can I change it so it will accept all image file types (JPEG, JPG, PNG, GIF)

thats just the filename, not the actual data. i don’t see why you would use base64 at this point, just echo the filename within the images “src”-attribute. otherwise you have to file_get_contents() first, but this will blow up the size of your sourcecode and prevent caching mechanisms

How would I go about doing that? When I try to use the code:
<img src="' .$avatar. '" class="user_avatar" alt="user avatar" />
… it continues to display nothing but the alt="" of the image :frowning:

that makes no sense - if the path to the file is wrong, base64 would not change anything, even if you get the actual image data, which you won’t get, because the path to the image file is still wrong. just right-click on the image, open in new tab or something, look at the wrong path, correct the path to this image, and apply these changes to the img’s src-attribute.

I am storing my images inside of a blob in the database (it’s not in any directory / path). I am using the $avatar inside of the src="" tags because:

$username = $_SESSION['username'];

// run query to get users avatar
$getAvatar = dbConnect()->prepare("SELECT * FROM users WHERE username = :username");
$getAvatar->bindParam(':username', $username);
// execute query
$getAvatar->execute();
// set each column as row
$row = $getAvatar->fetch(PDO::FETCH_ASSOC);
// set $avatar as users set avatar
$avatar  = $row['avatar'];

What is $avatar = $_POST['avatar'];

A String or an image file?

$avatar = $_POST['avatar'] is in the registration page which is tied to the form where users are registering from, specifically the

<input type"file" name="avatar" />

part of the form. From there I run a query where it inserts all the inputs into the databse table users:

$avatar = $_POST['avatar'];

$activation_id = uniqid(true);
$query = dbConnect()->prepare("INSERT INTO users (username, password, email, activated, activation_id, avatar) VALUES (:username, :password, :email, :activated, :activation_id, :avatar)");
$query->bindParam(':username', $username);
$query->bindParam(':email', $email);
$query->bindParam(':password', $hash);
$query->bindValue(':activated', "0");
$query->bindValue(':activation_id', $activation_id);
$query->bindParam(':avatar', $avatar);
$query->execute();

in the case that you store blobs in your database $avatar should be some human unreadable set of fuzzy characters - and not a filename with .jpg as extension - and users usally don’t put binary data in form fields. and uploads come from $_FILES

2 Likes

Do you know where I am going wrong as $avatar simply is echo’ing the actual file name & extension like: rick2.jpg :frowning: One thing I left out; here is the structure of the users database:

https://puu.sh/sqAEH/a521fdf636.png

you’re not storing the actual data, your input misses an = after the type, maybe resulting in a default type="text" field. But i don’t know how the filename gets into that, looks like browser behavior. set the attribut right, then get the file from $_FILES, have alook at it first with print_r(), and if you really want to store a blob take file_get_contents() for this.

2 Likes

I just looked over my code and cannot find where you see an input missing a =. I have looked over registration page and my functions but cannot find it. Earlier yesterday I have made these changes and was successfully able to display users avatars using the <img src="data:image/jpeg;base64,' . base64_encode($avatar) . '" class="user_avatar" alt="' .$username. ' avatar" /> statement but made more changes today and seemed to break it somehow. It’s not as simple as just Ctrl + Alt + Z 'ing either as I worked a lot today and not sure how far back the correct code is to undo that far.

That is my apologies, in my code it is indeed: <input type="file" name="avatar" />

have a look at the database table (select * from users): do you see blobs as fuzzy characters or filenames? phpmyadmin should convert that if possible.

The only thing that is being displayed is the columns, as it would be if I just hit browse on the table:
https://puu.sh/sqBer/c5dcbde2c9.png

have a look at + options if you could switch to displaying actual blob data

but still, don’t ignore that:

uploads come from $_FILES

With that checked the blob column now looks like the following:


… so to answer your question I see the blobs as filenames, not fuzzy characters :frowning:

When you say uploads come from $_FILES; how should I be incorporating that into my query / database / page to echo the user’s avatar correctly. As I said before even without using the $_FILES yesterday evening, it was successfully echo’ing the users avatar fine but do not remember how the code was looking then…

$_FILES is not about displaying the images, it’s about how you get them into the database, and this

$avatar = $_POST['avatar'];
...
$query->bindParam(':avatar', $avatar);

won’t get the actual file, it will just get any input text/select/whatever-is-NOT-an-upload form field. just have a look at

var_dump($_POST);
var_dump($_FILES);

before your insert something.

OK so how would I about actually inserting the image into the database (not just the file name as it is doing right now).