Jump to content

Renaming an image in PHP


angelali

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.