angelali Posted March 4, 2012 Share Posted March 4, 2012 Hello, I made a site where to upload images. The images are saved to a directory (folder). After someoe uploads an image, he gets the link. For example, if someone has an image called "xxx.jpg", he gets the url like this: www.example.com/upload/xxx.jpg. What I want is, when someone uploads an images, the name of the image change to random numbers. I know the PHP function rand() and rename () function, but the only problem is, it is renaming the file without considering the file extension. Here is a sample of my codes: $image = htmlentities($_FILES['file']['name']); $number = rand(1,1000); $newname = rename ($image, $number); echo $newname So, can someone tell me, how to rename images but taking into consideration of the file extension? If the image is xxx.jpg, I want it like 123.jpg not 123. Quote Link to comment https://forums.phpfreaks.com/topic/258267-renaming-an-image-in-php/ Share on other sites More sharing options...
requinix Posted March 4, 2012 Share Posted March 4, 2012 First, I have a question to you: what if there's already a file named with that random number? Quote Link to comment https://forums.phpfreaks.com/topic/258267-renaming-an-image-in-php/#findComment-1323857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.