/    Sign up×
Community /Pin to ProfileBookmark

Why do big sites rename image file names when uploaded? Like 4h34jkkj.jpg

If you right click on images on myspace, facebook etc, the image you uploaded always comes out with a string of numbers and character like:

7dfg67dg6dgd8.jpg

Do they use an algorithm for this? How can they be sure the string is unique? Is it incremented each time it’s run? How would you keep the variable alive?

I searched around a bit and couldn’t find the answer.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 04.2008 — It's done to prevent naming conflicts, more often than not it's a part of a hash (go google MD5 for a really good sample of what I mean), if two images have the same hash then there is a pretty good chance they are the same image, if not a suffix/prefix can be applied as needed, this keeps duplicates off the server(saving space), removes naming conflicts(you don't want 50 people calling their image the same thing) and all in all just makes the images easier to maintain.
Copy linkTweet thisAlerts:
@SyCoDec 04.2008 — Another reason is security. Should a poorly written script allow a file, like image.gif.php, be uploaded to a public folder then the user who uploaded it can now call the image file directly and execute any hidden code. Random image names means even the user doesn't know the name of the file to call.

Uploading images to non web browseable folders, renaming both the file and the extension and calling them and rebuilding the name/ext with a script and database helps secure things too.
Copy linkTweet thisAlerts:
@TopkatauthorDec 04.2008 — Ahh, so if 5 friends on facebook all upload the same file, they can all share the same URL to display the photo and it's only saved once...

Thanks for the answers.
Copy linkTweet thisAlerts:
@SyCoDec 05.2008 — Ahh, so if 5 friends on facebook all upload the same file, they can all share the same URL to display the photo and it's only saved once...

Thanks for the answers.[/QUOTE]


If they all upload the same file eg. me.jpg, it will save 5 files all with unique names. With the random names it can save them all into a single folder and not worry about naming conflicts.

If you can link remotely to an image, then the person who uploaded the image can share the image URL but only one image is uploaded.

The URL would have to be different for each image if called from a web browseable folder. If the image is uploaded from behind a user login system then it could save the userid, the unique new name and the original image name on image upload and store the image out of the web browseable area. Based on the users account or URL eg bobspage.myspace.com the script can look at the unique identifier, in this case 'bobspage', look up the userid and the associated images. It can call the image from the non web browseable file, so the image url might end up looking like

bobspage.myspace.com/displayimage.php/me.jpg

The script is executed at the .php part, rebuilding the image from a non web browseable folder, but the browser thinks it's a jpg.

So the no, URLs cant be the same for files uploaded by different people, but files of the same name can be uploaded.
×

Success!

Help @Topkat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...