Ell20 Posted December 30, 2007 Share Posted December 30, 2007 Hi, I have an upload script which successfully uploads a picture and creates a thumbnail of the picture and stores the information in the database. I have just used the file_exists function to give out an error message if a user uploads a photo which has the same name as another photo in the directory. But what I would ideally like to do is to create some kind of unique image name so that is either unlikely or impossible to have 2 images with the same name. I have had a look at various unique image naming scripts online but none of them seem to fit my code as I am using the copy function. copy ($_FILES['imagefile']['tmp_name'], "/home/mysport/public_html/images/UserImgs/".$_FILES['imagefile']['name']) or die ("Could not copy"); Appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/ Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 $_FILES['imagefile']['name'] = md5($_FILES['imagefile']['name'] . rand()) . $_FILES['imagefile']['name']; That would make something unique. Very unlikely that it would be duplicated. Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426237 Share on other sites More sharing options...
Ell20 Posted December 30, 2007 Author Share Posted December 30, 2007 Thanks its worked as I have echoed the name of the file but I also have an error: An error occured in script /home/mysport/public_html/includes/functions.php on line 87: Undefined index: extension Line 87 of functions.php: if ( strtolower($info['extension']) == 'jpg' ) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426239 Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 Show us your entire code. Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426242 Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 if you want a really unique name just use the database ID of the row you are inserting Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426243 Share on other sites More sharing options...
Ell20 Posted December 30, 2007 Author Share Posted December 30, 2007 gallery.php <?php if (isset($_POST['submit'])) { if ($_FILES['imagefile']['type'] == "image/jpeg"){ copy ($_FILES['imagefile']['tmp_name'], "/home/mysport/public_html/images/UserImgs/".$_FILES['imagefile']['name'] = md5($_FILES['imagefile']['name'] . rand()) . $_FILES['imagefile']['name']) or die ("Could not copy"); echo '<h3><center>Picture Uploaded!</center></h3>'; Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426244 Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 You're function needs to be ABOVE your code, its below at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426247 Share on other sites More sharing options...
Ell20 Posted December 31, 2007 Author Share Posted December 31, 2007 function.php is called in at the top of the page? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426250 Share on other sites More sharing options...
papaface Posted December 31, 2007 Share Posted December 31, 2007 I can't see it.... Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426252 Share on other sites More sharing options...
Ell20 Posted December 31, 2007 Author Share Posted December 31, 2007 Thats because I didnt add all the code in just the bits in question. include_once ('includes/functions.php'); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426255 Share on other sites More sharing options...
papaface Posted December 31, 2007 Share Posted December 31, 2007 Edit: Where is the filename it is supposed to be getting the extension from? Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426257 Share on other sites More sharing options...
Ell20 Posted December 31, 2007 Author Share Posted December 31, 2007 Fixed. I simply moved where the thumbnails were being created to after the information was inserted into the database. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/83775-solved-give-image-unique-name/#findComment-426259 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.