user1276898
user1276898

Reputation: 9

How to store and retrieve pictures using PHP?

I am building a webpage using PHP to search within my database. I am able to do a query and get the data to show on my page.

I also have a picture that is related to each record. Let's say something like a picture ID for each employee. I am wondering what is the best way to store and retrieve pictures in order to get it show using PHP.

Is there a way to store all the pictures in the subfolder like " image" and retrieve the pictures using PHP? I tried to search around on the internet, but I get confused.

Also, when I type something in the search box and click the button, is there a way to keep the input in the search box without erasing it?

Thank you very much.

Upvotes: 0

Views: 782

Answers (2)

Shaunak
Shaunak

Reputation: 18018

You can store the pictures in database itself. You can have a field in the user table called 'pic' with data type 'blob'. This can store the image for every user. This link will get you started:

http://mrarrowhead.com/index.php?page=store_images_mysql_php.php

As, correctly pointed out by @shaheer in the comment below, this is not a recommended practice. 'Attaching' images to your class objects by storing the web-url in the database as a attribute of that class is a better way. There is a excellent Ruby gem that I use for this frequently its called Carrierwave. Its workflow should be good for you to get Idea on how to do this in php.

I am not sure if there are any good alternatives for php (I am not a php dev) but a quick search returned this: Attachy. This is specifically to manage 'uploads', but you can use the same technique on existing images that you want to attach to your class.

Upvotes: 1

Collin Green
Collin Green

Reputation: 2196

The typical way is exactly what you said - store all the pictures in a folder and store their path in the database (or be able to build their path from their id or something). You just need to create a script to insert the record into your database when you add the picture to the file system.

Upvotes: 1

Related Questions