Page 1 of 1

How can I rename image before uploading?

Posted: Wed Jun 06, 2007 12:39 am
by amir
Hello,

How can I rename image before uploading. I first insert image path/name into DB and then image into a directory on server. Now, if two user comes on my site and one is male and other is female and they both uploaded their display pictures. First male uploads with the name male.jpg and then female uploads with the same name then it overwrites the image and users don't see their actual picture. I don't want to create new folder for each user. I just want to first get ID of image I just inserted into DB as its auto-incremental and then concate that id with the image name and then upload. e.g. image_1.jpg, image_2.jpg and so on.

Thank you so much
Aamir

Posted: Wed Jun 06, 2007 12:42 am
by Christopher

Posted: Wed Jun 06, 2007 12:43 am
by s.dot
Well, select the max(`id`) from the database, and implement it by 1, and name the picture that.

Separate folders for each user is actually a good idea. Or, something that could almost guarantee a unique name for an image.

Something like...

Code: Select all

$img_name = time().substr(md5(microtime()), 0, rand(5,12)).$img_extension;
Or, play around with uniqid()

Posted: Wed Jun 06, 2007 12:57 am
by amir
thanks
i tried this and it works

Code: Select all

$id = 99;
	$image = $id.'_'.$_FILES['image'][name];
	$path = "testimage/";
	
	if (move_uploaded_file($_FILES['image']['tmp_name'], $path.$image))