13 Replies - 1960 Views - Last Post: 10 November 2008 - 08:57 PM Rate Topic: -----

#1 jofin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-September 08

Saving images in database(profile pics)

Posted 10 November 2008 - 01:55 AM

How can i accept profile pictures from users and display it with their profile???
Is This A Good Question/Topic? 0
  • +

Replies To: Saving images in database(profile pics)

#2 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 03:46 AM

Don't.

Save the images in a file, and save the file reference in the database.
Was This Post Helpful? 0
  • +
  • -

#3 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 87
  • View blog
  • Posts: 796
  • Joined: 25-February 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 12:29 PM

Just to clarify, what Hary is talking about is saving the file name in the database and then pulling that whenever you need to display the image.

You would have a field called "profileImage" or something to that sort. When a user uploads their picture you get the file name and check to make sure a file with that name doesn't exist. Do any other error checking, and then when you input the user's information into the data, you would insert the file name into profile image. The file gets moved to a directory that holds all the user images. When you generate the user's profile, simply pull the file name and build do something like this:

<img src="path_to_profile_images/<?= $row['profileImage']; ?>">



You don't want to store files in your database, just file name references.
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 04:12 PM

View Postakozlik, on 10 Nov, 2008 - 11:29 AM, said:

When a user uploads their picture you get the file name and check to make sure a file with that name doesn't exist.


The post above is good, but the only recommendation I'd make over it is to have the file name randomize into, say, a random 9 digit number, keeping the original file's file extension. Then, there's less of a chance of file naming issues :)

Of course, you'd still want the code there to check for duplicates.
Was This Post Helpful? 0
  • +
  • -

#5 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 87
  • View blog
  • Posts: 796
  • Joined: 25-February 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 04:14 PM

View PostValek, on 10 Nov, 2008 - 06:12 PM, said:

the only recommendation I'd make over it is to have the file name randomize into, say, a random 9 digit number, keeping the original file's file extension. Then, there's less of a chance of file naming issues :)


If you really want to make sure you get a random file name that won't be uplicated you could always hash the timestamp and use that as the filename.
Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 04:15 PM

That's an even better idea :)
Was This Post Helpful? 0
  • +
  • -

#7 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 04:46 PM

hashing the timestamp takes into account the date as well correct?

Just time could give a duplicate but I'm pretty sure timestamps have date and time correct?
Was This Post Helpful? 0
  • +
  • -

#8 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 87
  • View blog
  • Posts: 796
  • Joined: 25-February 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 04:49 PM

Right, the timestamp should give you a large integer which is the amount of time that has elapsed since the unix epoch. Since this number is always different, and since hashing will never (hopefully) create a duplicate hash from two separate sources, that should be a safe way to encode your image names.
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 05:07 PM

Exactly. THe only way to duplicate it would be to have two people upload an image at precisely the same second, but one way around that one would be to have it hash the same timestamp with a different hash type. For instance, if you're using MD5 and that filename is already there, have it hash in SHA1. Then you still have a unique filename for it without overlap :)
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 05:09 PM

I believe it is impossible to have two identical timestamp values, as even if someone uploaded at 12:01:57 the milliseconds would be different, resulting in a different timestamp
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 05:15 PM

time()

It only counts seconds, not milliseconds.

This post has been edited by Valek: 10 November 2008 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 05:19 PM

Well I stand corrected, never seen a Time functions that didn't count milliseconds before. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#13 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 514
  • View blog
  • Posts: 1,604
  • Joined: 08-November 08

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 05:20 PM

It caught me off guard when I started using PHP. I figured it out when I was testing differences for a caching function and noticed an hour was 3600 :P

This post has been edited by Valek: 10 November 2008 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

#14 xerxes333  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: Saving images in database(profile pics)

Posted 10 November 2008 - 08:57 PM

Well you could use microtime() assuming you meet the criteria
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1