Solar Posted January 13, 2009 Share Posted January 13, 2009 How can I make it so when someone uploads an image, that it will rename the file to their name. Eg. URL/photo/Solar.gif I can't figure it out <?php $username = $_SESSION['username']; $target = "./photo/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140680-renaming/ Share on other sites More sharing options...
dennismonsewicz Posted January 13, 2009 Share Posted January 13, 2009 http://www.phpeasystep.com/workshopview.php?id=18 Quote Link to comment https://forums.phpfreaks.com/topic/140680-renaming/#findComment-736227 Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 change $target $target = $target . basename( $_FILES['uploaded']['name']) ; to $target = "./photo/"; $path = pathinfo($_FILES['uploaded']['name']); //gather file data $targetnew = $target.$_SESSION['username'].$path['extension']; while(file_exists($targetnew)) { //make sure the file doesnt already exist $targetnew = $target.rand(0,1000).$_SESSION['username'].$path['extension']; } $target = $targetnew;unset($targetnew); I havnt tested but looks ok to me. Quote Link to comment https://forums.phpfreaks.com/topic/140680-renaming/#findComment-736245 Share on other sites More sharing options...
Solar Posted January 13, 2009 Author Share Posted January 13, 2009 It works! But theres a missing . in there. Random picture uploaded as; Solarpng It missed its . extension. Imma look before you reply as well Nevermind dawsba, I've figured it out. Thanks a lot!! Quote Link to comment https://forums.phpfreaks.com/topic/140680-renaming/#findComment-736251 Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 lol sorry about that wrote it off the cuff, have fun Quote Link to comment https://forums.phpfreaks.com/topic/140680-renaming/#findComment-736258 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.