How to display images in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jagannathg
Forum Commoner
Posts: 34
Joined: Wed Dec 08, 2010 1:55 am

How to display images in PHP?

Post by jagannathg »

Hello All,

I wan to display images from the MySQL. Let me me explain you hows my site structure is. My site has a login page where all the users login. The same page has the Registeration Page where the New Users can register themself. NOw I am done with Login, Registration and displaying info like Username, there First and Last Name after they login in the members page.
In the members page I have a Link which says "Edit Profile" and when the users click on this link they can update there information like Email Id, Phone, Location, etc. Now here I want to incorporate the feature of "Upload Profile Pic" so that it can be displayed on there Members Page when they login.

My Database now has no fileds to capture the image information, but when I googled I found that I need to have two main field that is 1. Image Name (where i will store the Imagename) and 2. Image (where I will store the actuall Image) I found and checked we need to use BLOB.

I kindly request to all to help me how to upload and display images in my members page. Let me now if still any other information is needed.

Regards,
Jagannath
jagannathg
Forum Commoner
Posts: 34
Joined: Wed Dec 08, 2010 1:55 am

Re: How to display images in PHP?

Post by jagannathg »

The same page has the Registeration Page where the New Users can register themself.
Sorry its not a page I have a hyperlink "Register" for the New Users on my login page.

Regards,
Jagannath
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How to display images in PHP?

Post by klevis miho »

I would not suggest you to store images in a database. Instead just store the image name in the database.
And the image itself in a folder.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to display images in PHP?

Post by pickle »

klevis miho wrote:I would not suggest you to store images in a database. Instead just store the image name in the database.
And the image itself in a folder.
+1

The filesystem is much faster at serving up images than the database. You'll notice a speed increase storing images in the filesystem compared to the database.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: How to display images in PHP?

Post by superdezign »

I find the best way to deal with profile images is to have a MySQL column to store the name of the image file (i.e. image) and a folder to hold all profile pictures (i.e. imgs/profile). Then, when they upload their profile picture, upload the file to your temporary folder (PHP does this automatically) and then save the uploaded file as /imgs/profile/{username}-{date}.jpg, where {username} is their unique user name and {date} is a timestamp or incremental value. The suffix exists for cache handling. You could just save it as {username}.jpg, but then you would have to build in cache control for your images to ensure that users are always served the latest version.

After that file is created, look in your database for the user. If there is a value in the image column, delete (unlink()) the file that is referenced, and then update the user's row with "{username}-{date}.jpg" in the image column.

In order to display the image on a page, pull the data from the user's row and simply output an <img /> tag.

Code: Select all

$result = mysql_query("select * from users where id=$id");
$user = mysql_fetch_object($result);

echo "<img src='/imgs/profile/{$user->image}' alt='Profile image for {$user->name}' />"; 
jagannathg
Forum Commoner
Posts: 34
Joined: Wed Dec 08, 2010 1:55 am

Re: How to display images in PHP?

Post by jagannathg »

Hello All,

Thank you for all your suggestions. I will try to start coding on uploading and displaying images. If I get stuck surely I will get back to this post and take all your help.

Regards,
Jagannath
Post Reply