How can I rename image before uploading?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

How can I rename image before uploading?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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()
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post 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))
Post Reply