ShoeLace1291 Posted October 6, 2007 Share Posted October 6, 2007 Ok, I have an upload script that allows users to upload images to the server/database. My problem is that if a user uploads a file with the name of a file that already exists on the server, it overwrites the existing file. If I have a script that randomly generates 3 letters/numbers, how would I insert those in between the file name and the file extension? I'm thinking the solution would have something to do with implode, but I'm not sure how I would use it. I'm using the $_FILES['upfile']['name'] variable. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/ Share on other sites More sharing options...
Gibbs Posted October 6, 2007 Share Posted October 6, 2007 You can check if the file exists by using the file_exists function prior to uploading. Never done this so I can't be 100% sure. With the move_uploaded_file function you can specify the path. For example: move_uploaded_file($_FILES['upfile']['name'],<where you want it/filename>) Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363044 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 use http://www.php.net/manual/en/function.rename.php then concat your randomize letter or number if (file_exists(filename)){ rand() //etc... rename('oldfile', $randomizedletterornumber.yourfilenametoupload) } Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363045 Share on other sites More sharing options...
ShoeLace1291 Posted October 6, 2007 Author Share Posted October 6, 2007 Will something like $_FILES['upfile']$combo['name'] work? Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363049 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 try it and expect errors lol i guess no it wont work Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363051 Share on other sites More sharing options...
ShoeLace1291 Posted October 6, 2007 Author Share Posted October 6, 2007 Well since $_FILES['upfile']['name'] displays for example name.gif, I need something that would only display "name" without the extension. Is there any way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363056 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 str_replace(".gif","","$_FILES['upfile']['name']"); but y? Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363058 Share on other sites More sharing options...
ShoeLace1291 Posted October 6, 2007 Author Share Posted October 6, 2007 Well the script needs to rename the file to say filenamea4u.gif if the name filename.gif already exists. Kinda like what image shack does. Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363060 Share on other sites More sharing options...
teng84 Posted October 6, 2007 Share Posted October 6, 2007 use http://www.php.net/manual/en/function.rename.php then concat your randomize letter or number if (file_exists(filename)){ rand() //etc... rename('oldfile', $randomizedletterornumber.yourfilenametoupload) } is this wrong? Quote Link to comment https://forums.phpfreaks.com/topic/72044-renaming-a-user-selected-file/#findComment-363062 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.