Vinay Kumar Sharma
Vinay Kumar Sharma

Reputation: 43

What is the correct way for nomenclature of millions of images?

I have a image upload form which would upload large (may be millions) of records. Something like profile images of users, Currently we are giving the nomenclature as

'some_constant_string'.time().[image_file_extension];

for eg. 'tcs'.time().'jpg'; Does it a right approach or it will produce some duplicacy by the time. Please help on the correct way to do it!

Currently Using PHP 5.6 with Codeigniter Framework 3.0

Upvotes: 0

Views: 72

Answers (4)

darode
darode

Reputation: 140

If you expect a lot of files to be uploades I would change the approach a little.

I will change the naming to something like this:

<user_id> . '_' .microtime(true) . <img_extension>

Then you will have less problems with duplicate images because two images for the same user should be processed in the same microsecond. If you think you could still having problems you could try with this:

<user_id> . '_' . microtime(true) . '_' . <inc> . <img_extension>

Here the value of could be a counter in soee a loop to rename images or an auto increment field in a db id you need a centralized approach.

Another think you should take care is how to store all the images. Putting all of then in the same folder is a bad idea so in adittion to the name I will recommen to create a forder structure, for example:

You have this file:

123_1498116500_1.jpg

You can split to a folder structure like this:

/123/149/811/650/123_1498116500_1.jpg

With this structure you will avoid some overhead when the operation system should manages folders with thousand of files.

Hope it helps :)

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146578

Since time() has (obviously) a one-second resolution, you'd need to throttle your image processing to one per second. But a day has only 86,400 seconds on average—just do the maths.

uniqid() should be a better alternative—don't forget to enable the $more_entropy parameter.

Upvotes: 0

Martijn
Martijn

Reputation: 16123

That is not the right approach. If two users fire the upload event at the same moment, they both'll have 1234.jpg, resulting in at least one confused user.

For unique names, there is uniqid(). That'll fix your current problem.

But, there is another (IMO better) solution: Use proper dirnames, e.g. /uploads/user_id/some-image.jpg. This'll give you a lot more advantages.
At the very least, duplicate filenames is (nearly) impossible. You can now also use functions like scandir() a lot more efficient, as you dont have to check huge amounts of images. And if you delete a user, you simply delete /uploads/user_id/ and you've easily removed all their images.

Upvotes: 1

Ryosaku
Ryosaku

Reputation: 465

the duplicacy of an image is possible, but it can happens only if 2 or more users upload an image at the same time. You can use an MD5 (or something else) to identify uniquely your image, or use something like uniqid()

Upvotes: 0

Related Questions