0
896ba38b-1808-49de-ace0-c079aed65c5a_f.jpg

I need to generate a random string like above to name images. Basically I don't want to use a predictable format where people you easily script to extraction of all the images from the site I'm building.

I know the PHP rand() function.

To make a string like above would I be best to just use the rand() function many times?

better way to do this? thoughts?

thx

2 Answers 2

2

Try using the uniqid() function:

http://php.net/manual/en/function.uniqid.php

This generates universally unique IDs like what you are after. Maybe you want to stitch together 2 or more of these, if you want longer strings.

EDIT: You also probably want to set $more_entropy argument to true.

Sign up to request clarification or add additional context in comments.

Comments

1
$rand_val = date('YMDHIS') . rand(11111, 99999);
echo $image_file_name = md5($rand_val) . '.jpg';

// 9da5ea8c52b14305fd11a9c012a23f08.jpg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.