Multiple user picture uploads

Hi guys!

I’m creating a profile system which the user can upload multiple pictures to his/her profile and also they are able to display chosen pictures on other user profile pages.

The way in which i’ve created it so far is when the user uploads an image, it automatically creates them their own directory in the server.

$oldmask = umask(0);
        if (!is_dir('imageuploads/USER_IMAGES_ID'.$_SESSION['user_id'])) {
          mkdir('imageuploads/USER_IMAGES_ID'.$_SESSION['user_id'],0777,true);
          }
          umask($oldmask);

Would there be any problems doing this when i have a large community?

Also, how would be the best way to allow them to have multiple images displayed on their profile? At the moment it sends all the images correctly to the server but only inputs the latest uploaded image to the database because i’m using UPDATE.

    mysql_query("UPDATE uploads SET user_images='$uploadfile' WHERE id='$id'");

It will display something like this in the database table - user_images/USER_IMAGES_ID10/image1 Which works fine and i am able to display the image anywhere i want.

However I could use something like INSERT but wouldn’t that just create a long string of text something like user_images/USER_IMAGES_ID10/image1user_images/USER_IMAGES_ID10/image2 which would display an error on the user profile?

Hope you guys understand what i’m trying to ask here :stuck_out_tongue:

I see no problem creating a image directory for each user.

I would just store the image name(s) in the DB as a string separated by | then you can explode when needed to display images. For an update I’d query to get any images they might have, then add the new one in and update the table. Something like this.

<?php		  	
	//Query table for user images
	$sql = "SELECT user_images
	FROM `uploads`
	WHERE id='$id'";
	$result = mysql_query($sql) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
		$images = $row['user_images'];		
	}
	//Turn string into array
	$oldimages = explode('|',$images);
	foreach($oldimages as $key => $value){
		//run a little check to get rid of double ||
		if(empty($value)){
			unset($oldimages[$key]);
		}
	}
	
	$newimagearray = array_push($oldimages,$uploadfile);
	$newimagearray = array_unique($newimagearray);
	
	
	$ImageStr = implode("|", $newimagearray);
	$ImageStr = rtrim($ImageStr, '|');
	$q1 = "UPDATE uploads set
	user_images = '$ImageStr'	
	where id='$id'";
	mysql_query($q1) or die(mysql_error());	
	}
?>

Thanks for that!

I’m getting the following errors -

Warning: array_unique() [function.array-unique]: The argument should be an array
Warning: implode() [function.implode]: Invalid arguments passed

Hey sorry about that. Take a different approach.

<?php
    //Query table for user images
    $sql = "SELECT user_images
    FROM `uploads`
    WHERE id='$id'";
    $result = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        $images = $row['user_images'];
    }
    //Turn string into array
    $oldimages = explode('|',$images);
	
	// Start new image array
	$newimagearray = array();
    foreach($oldimages as $key => $value){
       $newimagearray[] = $value;
    }
	//Add new image
    $newimagearray[] = $uploadfile;
	 		
	// Make sure there's no duplicates
    $newimagearray = array_unique($newimagearray);

  	// Turn image array back into string with | separator
    $ImageStr = implode("|", $newimagearray);
	
	//Update Table
    $q1 = "UPDATE uploads set
    user_images = '$ImageStr'
    where id='$id'";
    mysql_query($q1) or die(mysql_error());

?>