madness69 Posted October 17, 2012 Share Posted October 17, 2012 Hello there, i have a image upload that its not perfect bu it already enought for what i need, but there is a problem, the name of the image that i uploaded is always the same, and for some reason i upload a image whit same name it will overwright the image, i need to put my image upload code to give each image a unique name, here is the code to take a look. What is missing to accomplishe this? Best reagards $name=$_FILES['image']['name']; $tmp=$_FILES['image']['tmp_name']; $err=$_FILES['image']['error']; if($err==0) { move_uploaded_file($tmp, "../upload/$name"); } $name=$_FILES['image2']['name']; $tmp=$_FILES['image2']['tmp_name']; $err=$_FILES['image2']['error']; if($err==0) { move_uploaded_file($tmp, "../upload/$name"); } $img=$_FILES["image"]["name"]; $img2=$_FILES["image2"]["name"]; Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/ Share on other sites More sharing options...
beyzad Posted October 17, 2012 Share Posted October 17, 2012 Hi. Why you dont wanna rename your image? if you seriously dont want to rename your image, Then you can make a unique directory for each image. Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385768 Share on other sites More sharing options...
White_Lily Posted October 17, 2012 Share Posted October 17, 2012 @beyzad - re-read his problem. he WANTS each image to have a unique name. Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385772 Share on other sites More sharing options...
ManiacDan Posted October 17, 2012 Share Posted October 17, 2012 (edited) Instead of taking $name from the file info, give it a new name. uniqid produces a unique string. Edited October 17, 2012 by ManiacDan Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385779 Share on other sites More sharing options...
madness69 Posted October 17, 2012 Author Share Posted October 17, 2012 Instead of taking $name from the file info, give it a new name. uniqid produces a unique string. In practice how would i insert uniqueid to my code? $name=$_FILES['image']['name']; $tmp=$_FILES['image']['tmp_name']; $err=$_FILES['image']['error']; if($err==0) { move_uploaded_file($tmp, "../upload/$name"); } $name=$_FILES['image2']['name']; $tmp=$_FILES['image2']['tmp_name']; $err=$_FILES['image2']['error']; if($err==0) { move_uploaded_file($tmp, "../upload/$name"); } $img=$_FILES["image"]["name"]; $img2=$_FILES["image2"]["name"]; Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385833 Share on other sites More sharing options...
ManiacDan Posted October 17, 2012 Share Posted October 17, 2012 See where you take $name from the file info? Instead of taking $name from the file info, give it a new name. uniqid produces a unique string. Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385844 Share on other sites More sharing options...
JohnTipperton Posted October 18, 2012 Share Posted October 18, 2012 Hello there, i have a image upload that its not perfect bu it already enought for what i need, but there is a problem, the name of the image that i uploaded is always the same, and for some reason i upload a image whit same name it will overwright the image, i need to put my image upload code to give each image a unique name, here is the code to take a look. What is missing to accomplishe this? Best reagards $name=$_FILES['image']['name']; $tmp=$_FILES['image']['tmp_name']; $err=$_FILES['image']['error']; if($err==0) { move_uploaded_file($tmp, "../upload/$name"); } $name=$_FILES['image2']['name']; $tmp=$_FILES['image2']['tmp_name']; $err=$_FILES['image2']['error']; if($err==0) { move_uploaded_file($tmp, "../upload/$name"); } $img=$_FILES["image"]["name"]; $img2=$_FILES["image2"]["name"]; try to replace the actual name to a certain algorithm such as userid the time and date so the filename will be unique code should be like this $UploaderID=1 // assume the value of the uploader in the database is 1 $GeneratedFilename = $Uploader + date("Ymd"); // then this code will generate the filename. Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385951 Share on other sites More sharing options...
ManiacDan Posted October 18, 2012 Share Posted October 18, 2012 Don't use that solution, it's not unique. Use mine. Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1385979 Share on other sites More sharing options...
JohnTipperton Posted October 18, 2012 Share Posted October 18, 2012 (edited) Don't use that solution, it's not unique. Use mine. opps sorry i forgot to add the time :D but uniqid i prefer Edited October 18, 2012 by JohnTipperton Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1386031 Share on other sites More sharing options...
ManiacDan Posted October 18, 2012 Share Posted October 18, 2012 The time is not unique either, if the form has two file uploads, they'll be processed in the same second. "Use microtime" you'd say. uniqid() is designed to solve this problem. Quote Link to comment https://forums.phpfreaks.com/topic/269576-unique-name-to-images/#findComment-1386047 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.