2 Replies - 513 Views - Last Post: 01 July 2016 - 07:31 AM Rate Topic: -----

#1 Relaxed_Bear  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 03-January 14

Is this a good way to store profile pictures?

Posted 01 July 2016 - 12:56 AM

Hello everyone,

While developing a social networking site for a school project, I came up with a quick way to store profile pictures. Now that I'm planning on using it again, I'm wondering if this method is actually efficient (or are there better alternatives?). Let me explain:

On my server, I have a folder called "thumbnail" with default.jpg in it. When a user uploads a photo, this particular photo will be stored in this map using his user_id as the name (Ex: 5.jpg will be stored if the user has user_id 5). When a user browses his profile (or that of someone else), I wrote a script to determine if this user has a profile photo or not:

//Profielfoto systeem
	if (isset($_GET['u']))
	{
		if (file_exists('../../thumbnail/' . $_GET['u'] . '.jpg'))
		{
			$pf = '\'../../thumbnail/' . $_GET['u'] . '.jpg\'';
		}
		else
		{
			$pf = '\'../../thumbnail/default.jpg\'';
		}
	}
	else
	{
		if (file_exists('../../thumbnail/' . $_SESSION['UserId'] . '.jpg'))
		{
			$pf = '\'../../thumbnail/' . $_SESSION['UserId'] . '.jpg\'';
		}
		else
		{
			$pf = '\'../../thumbnail/default.jpg\'';
		}
	}



This method has solved my problem fairly well but the main drawback I can think of is that old profile pictures will be deleted for good.

Back to my question: are there similar practices used in today's age or are there better practices? If so, can you point them out please?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Is this a good way to store profile pictures?

#2 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: Is this a good way to store profile pictures?

Posted 01 July 2016 - 07:11 AM

Standard practice is to store the path in a database and retrieve it on demand.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Is this a good way to store profile pictures?

Posted 01 July 2016 - 07:31 AM

Nope, nothing wrong with storing files on a file server. You needn't bother with an extra database entry if the application has a standard file naming convention based on data already stored, like user id. I would discourage "../.." though, as relative paths are nice security holes in web apps.

Also, you needn't repeat yourself so much. Make a function to return your thumbnail path. e.g.
function getThumbPath($id) {
  global $ROOT_PATH;
  return $ROOT_PATH + '/thumbnail/' . $id . '.jpg'
}

$thumbPath = getThumbPath(isset($_GET['u']) ? $_GET['u'] : $_SESSION['UserId']);
$pf = "'" . (file_exists($thumbPath)?$thumbPath:getThumbPath('default')) . "'";



Now, if in the future you choose to change you path naming convention, you only need to change it on one place.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1