Writing own php script for blog. Help with Images!

Hello all. I have figured out how to add and return text info from the mysql database. Now since this blog project will have images displayed, how do I go about adding and returning images from the mysql database?

Basically each post will have a couple of images displayed at the top and bottom. I can style them to be displayed like that, I just don’t know how you add images to the database and them retrieve them.

Personally I would store the image files in a separate folder on the server, and store their names only in the database. You’d then retrieve the names as part of the query that returns the text, and use them to build the image links. Will be interesting to hear the more experienced viewpoint.


See http://www.felgall.com/mysql06.htm for the code to insert and retrieve images from a database.

The alternative is to store them as images and simply store references to them in the database.

You should create folder with picture, in database put only name of picture, example picture.jpg or picture.png
Create connection and command to select picture name from database.
Then use variables with folder name and add function for retrieving picture name from database
$image = “images/”.namePicture();

Not necessarily. I have heard of sites that lost hundreds of thousands of pictures because the database was backed up and the pictures were stored separately.

Also you lose transaction functionality if the pictures are not stored in the database.

It all depends on what is most important.Only really small systems can usually get away with having the pictures outside the db.

One thing to keep in mind is if the images are stored in the database, your database backups are going to be much larger. Are the images to be loaded user avatars?

That’s why we backup our application, both ways are correct but I prefer using to store picture outside database in folder. This way is not overloading database search engine, image that you have 5000 users with minimum 5000 picture, and all that picture you put in database that a lot data in database.

Not really. It’s like a regular blog. It will have an image or two inside the content. Like for instance, if it’s an article on New York, it might have an image of the NY skyline right on top before the article begins and a carousel at the bottom for similar stories/articles.

I get your viewpoint. How can I add pictures to the mysql db? When I create a new table in phpmyadmin, there are plenty of options but none that suggest I could add images. Like for instance, you have INT, VARCHAR, TEXT, DATE, NUMERIC, DATE AND TIME, STRING AND SPATIAL.

I believe it’s known as a BLOB, which used to mean “Binary Large OBject” when I first heard the term. There are various, TinyBLOB, MediumBLOB and just BLOB on my admin page, which I assume have different size limits that you would have to consider before storing an image.

Thanks a lot. I went with BLOB. Tried medium and long and I can upload the image on there but when I download it from the database, it plays the image as a 10 second video. Also, when I fetch data using sql queries I can sucessfully get the text I need but not the image which shows up on my screen as gibberish like ‘ÿØÿàJFIFHHÿÛC aa    ÿÛC   ÿÂöî’ and so on.

What can I do next?

Sounds like you are retrieving binary data as plain text, eg. make a copy of an image and change the copy’s extension to “.txt” Open in a text editor. Is that what you’re seeing?

Yes, notepad++ gives me the same code. How can I fix that? Trying to google it but theres not much help available there. It seems you cannot just pull images out of the database as part of an array.

Are you using the appropriate Content-type header?

No, that is something I’ve never heard of before. I am only just getting the hang of php development. That link seems complicated.

What I have is this as the controller.

try {
	$sql = 'SELECT * FROM carousel';
	$result = $pdo->query($sql);
catch (PDOException $e) {
	echo 'Cannot retrieve results. Sorry.' . $e->getMessage;
$carousel_items = array();	
while ($row = $result->fetch()) {
	$carousel_items[] = array (
								'id' => $row['id'], 
								'title' => $row['title'], 
								'description' => $row['description'], 
								'image' => $row['image']
include 'carousel.php';

In carousel.php I have the foreach loop echoing $carousel_items as $carousel_item in between

  • tags.


    <?php foreach ($carousel_items as $carousel_item) : ?>
        <a href="single.php?id=<?php echo $carousel_item['id']; ?>">
        <?php echo $carousel_item['image'];?>
         <?php echo $carousel_item['title'];?> 
    <?php endforeach; ?>

    Where can I go about putting the content type header?

  • Have tried putting it in the controller after the $carousel_items array. At the bottom. Not working. I just don’t know how to use the Content-type header.

    I have used header (‘Location: .’) before and using Content-type in the same fashion isn’t working for me.

    I have made some changes to the code. It seems I have to fetch the images separately so I can use the content-type header so everything isn’t echoed as an image file. The images are now displaying. Thank you for that tip.

    How can I use contents of both arrays in the same foreach loop? I have looked up array_combine which is a bit confusing. Any thoughts?

    I’m glad you figued out at least part of it. :thumbsup:

    How to get it to work in the array was/is where I was/am getting stuck.
    (I don’t store images is the database, only their paths to the file.)

    But I am still thinking on it.

    When extracting an image from a database you can either extract it directly into a file (so it isn’t processed by the PHP at all) or have a separate PHP to extract it that you can substitute into the <img src="">

    Trying to create the image inline in the resultant HTML might be possible but it is many times more difficult.

    The reason I’m trying to use a database to store images even though I have been advised against it, is because I don’t want to manually be storing images in a folder on the server and submitting their file names to the db through a form. Is there a way I can use an upload form to upload images straight to the folder with their names going straight to the db? Or do I have to manually upload the images while the form takes care of the names?

    I am currently unaware of any system that allows for files to be sent directly to their respective folders on the server.