Adam
Adam

Reputation: 20952

PHP Generating a Random Number/String for Images

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

Upvotes: 0

Views: 2597

Answers (2)

Dino Babu
Dino Babu

Reputation: 5809

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

// 9da5ea8c52b14305fd11a9c012a23f08.jpg

Upvotes: 1

leftclickben
leftclickben

Reputation: 4614

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.

Upvotes: 2

Related Questions