glassfish Posted October 12, 2014 Share Posted October 12, 2014 (edited) The script for creating a new file name for the image: $validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) $file_extension = end($ext); //store extensions in the variable $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1]; $target_path = $target_path . $new_image_name;//set the target path with a new name of image The script creates a new file like: f6c9b8d9db05366c3504210cded9ddb2.jpg and moves the file to the "uploads" folder. And then the script also creates a thumbnail with the same file name and moves the file to the "thumbs" folder. The issue I am having is that the same ID code could happen again for a different image in the database, thus I would be calling a different original sized image than the thumbnail image. My question is: How to avoid this issue of the same ID code has happened again for a different file. What is the proper way to reference the anchor tag of the thumbnail image to its actual original sized image? With the script I have the thumbnail image would be coming from the "thumbs" folder and the anchor tag would get referenced to the "uploads" folder to get the original sized image. Edited October 12, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/ Share on other sites More sharing options...
ginerjm Posted October 12, 2014 Share Posted October 12, 2014 Same silly code again. When are you going to stop using it? As for ensuring no duplicates, how about just using a file system function to check if it already exists? Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493394 Share on other sites More sharing options...
glassfish Posted October 13, 2014 Author Share Posted October 13, 2014 (edited) As for ensuring no duplicates, how about just using a file system function to check if it already exists? Thanks for the answer. I am wondering, if there are a lot of images and the website has a "higher" traffic would that still work out? I am actually more wondering if it would "work out" or if problems would occur? Edited October 13, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493445 Share on other sites More sharing options...
ginerjm Posted October 13, 2014 Share Posted October 13, 2014 Are you opening up your own image museum? What are you worried about? What is "working out" supposed to mean? Basically what I see in your posts is : 1 - accept an image that you allowed someone to upload thru your form. 2 - rename that image and store it someplace To be safe you have asked how to ensure name integrity. You have been offered the quite simple solution of checking if that filename exists in your images folder before saving the new file there. Case closed? Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493460 Share on other sites More sharing options...
glassfish Posted October 13, 2014 Author Share Posted October 13, 2014 (edited) I did not think you would see this as "name integrity". Actually, I thought, perhaps there are alternative ways. With the way you suggested I was not sure if it still "works out" when the traffic is high. Edited October 13, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493468 Share on other sites More sharing options...
ginerjm Posted October 13, 2014 Share Posted October 13, 2014 You want to be sure that the name you create doesn't wipe out another file. I think 'name integrity' is an apt term. Dont' you? Your concern is about the possibility of two instances of your script creating the same random name at the same time. Even tho I don't quite follow your convoluted logic (and wasted logic) I dont' think that's going to happen. Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493470 Share on other sites More sharing options...
QuickOldCar Posted October 15, 2014 Share Posted October 15, 2014 How to avoid this issue of the same ID code has happened again for a different file. Append something like timestamp to the files name when saving it. If you want to store it as md5 after so be it, as long as you save what the image actually is in your database. $_SERVER['REQUEST_TIME'] will work What is the proper way to reference the anchor tag of the thumbnail image to its actual original sized image? It's supposed to display the sizes of the image are viewing. If you want to add additional info to original size...add it to alt or title, under the image on page, possibly even a js script like mouseover event. Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493556 Share on other sites More sharing options...
gizmola Posted October 15, 2014 Share Posted October 15, 2014 First thing that I see is that you could improve the variation in the uniqid, by using the optional parameter to increase entropy. Change uniqid() to uniqid('', true) The more variation of input the better the distribution of hashes. There is always a chance albeit infinitesimally small that you will have a collision (2 different inputs hash to the same hash). The chances of a collision on random data requires something around 2^64 hashes. You will never approach that number of files in your system, short of your system being at google/facebook-like scale. You could decrease this probability even more by using a larger hash -- sha1 being the most likely alternative. With that said, and as everyone has already pointed out -- if this is really important to you, then you can simply take the random name generated, and do a simple filesystem check to see if a file of the same name exists, and keep iterating until one does not. Practically speaking, the chances of it happening in your system are close to nil. Quote Link to comment https://forums.phpfreaks.com/topic/291588-original-image-size-thumbnail-image-anchor-tag-referencing/#findComment-1493560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.