I have a site where users can upload their avaters and i am having problems as the new uploded files are replacing filels in the images directory that has the same name. for eg. if user 1 has an avater called myavater.jpg and user 2 uploads diffrent picture but with the file name myavater.jpg, both users will end up having the last image uploaded as their avatar. To be honest with i have the smalest knoledge of PHP and your help is apreciated. I have copied the code below.

fileoperation.php

<?php
function resizeimage($file,$imageDir) {
	$userfile_name = $file['name'];

if(!isset($file['tmp_name']) || $file['tmp_name'] == '') {

}
if(copy($file['tmp_name'],"$imageDir/". $file['name']))
{
	/*
	 * let's start by identifying the image type. No doubt the more efficient way
	 * is to use string functions but who cares?
	 */

	$parts = split("\.",$file['name']);
	$ext = $parts[count($parts)-1];

	$thumb_name = array_slice($parts,0,count($parts)-1);

	$ext = strtolower($ext);
	switch($ext)
	{
		case "jpg";
		   /*
			* sometimes we may find that the image already contains  an
			* embedded thumbnail. Then we simply extract that.
			*/
			$thumb_data = exif_thumbnail("$imageDir/". $file['name']);
			$thumb_name = join(".",$thumb_name) .  ".jpg";
			if($thumb_data)
			{
				$fp = fopen("$imageDir/thumb/$thumb_name","wb");
				fputs($fp,$thumb_data);
			}



			else
			{
			   /*
				* tough luck here comes work.
				*/
				$src_img=ImageCreateFromJpeg("$imageDir/$userfile_name");
			}		
			break;

		case "gif":
			$src_img=ImageCreateFromGif("$imageDir/$userfile_name");
			$thumb_name = join(".",$thumb_name) .  ".gif";
			break;		

		case "png":
			$thumb_name = join(".",$thumb_name) .  ".png";
			$src_img=ImageCreateFromPng("$imageDir/$userfile_name");
			break;

	}

	/* get it's height and width */
	$imgSx = imagesx($src_img);
	$imgSy = imagesy($src_img);

	if($imgSy != 0)
	{
		/* 
		* lets calculate the aspect ratio and the height
		* and width for the scaled image.
		*/

		$ratio = $imgSx/$imgSy;
		if($ratio > 1)
		{

			$new_imgSx = 55;
			$new_imgSy = 55/$ratio;
		}
		else
		{

			$new_imgSx = (float) 55 * $ratio;
			$new_imgSy = 55;

		}

		$dst_img=imagecreatetruecolor($new_imgSx,$new_imgSy);

		/* create the scaled instance */
		ImageCopyResampled($dst_img,$src_img,0,0,0,0,$new_imgSx,$new_imgSy,$imgSx,$imgSy);

		/* write the damned thing to disk */
		if($ext == "jpg" || $ext == "gif")
		{
			imageJpeg($dst_img,"$imageDir/thumb/$thumb_name");				
		}
		else
		{
			imagePng($dst_img,"$imageDir/thumb/$thumb_name");				
		}
	}

/*		chdir("$imageDir/");
		if(strtolower($file['type']) == "gif") 
			rename($userfile_name.".jpg");
		chdir("/var/www/html/tahitizik/admin");
	*/	
	}
	else {
		echo "Unable to upload image";
	}
}	
?>

The page users upload their pictures has the following code: (createaccount.php)

	if($_FILES['txtphoto']['name'] != '') {
			$type = explode("/",$_FILES['txtphoto']['type']);
			if($type[0] == 'image') {
				resizeimage($_FILES['txtphoto'],"uploads/user");
				$objMember->photo=$_FILES['txtphoto']['name'];


		}
	}
	else {
		$objMember->photo = $_POST['txtOldImage'];
	}

    You should find a way to give each image its own unique name. You'll also need to make sure you remember which name goes with which user (perhaps in a database) one way to do this is to use [man]uniqid[/man] to name each image. Another way would be to name the image after some database index or something.

      I ave tried to change image name format in the database to integer and make it auto increment but it is not hapenning as there is only one unique data or primery key is allowed. I wold like to know how i can give it the unique id please.

        you could name the image after the existing primary key in your database. for instance, if your table is like this:

        MyTable
        id - primary key, autoincrement
        width- image width
        height - image height
        imageType - jpg, gif, png, etc.
        fieldx
        fieldy
        etc...

        then you could just use the id field to name your image using the id value, a '.' and, the imageType.

        That's kind of a pain though, because you have to create the data record before you know what name to give your file.

          Write a Reply...