Canman2005 Posted February 3, 2008 Share Posted February 3, 2008 Hi all I have a very simple fileupload script $code = rand(111111, 999999); $uploaddir = 'fileupload/'; $uploadfile = $uploaddir . $code. basename($_FILES['upfile']['name']); move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile); Is there a way to totally rename that file? So rather than the old filename, it would rename it to just the code. image33_234532.jpg would become 234532.jpg Any help would be great thanks in advance ed Link to comment https://forums.phpfreaks.com/topic/89120-renaming-file-upload/ Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 $code = rand(111111, 999999); $uploaddir = 'fileupload/'; $ext = explode('.',basename($_FILES['upfile']['name'])); $uploadfile = $uploaddir . $code . $ext[1]; move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile); Link to comment https://forums.phpfreaks.com/topic/89120-renaming-file-upload/#findComment-456429 Share on other sites More sharing options...
JacobYaYa Posted February 3, 2008 Share Posted February 3, 2008 First you are going to need to use substr with strrpos to grab the file extension then on this line $uploadfile = $uploaddir . $code. basename($_FILES['upfile']['name']); you will change to $uploadfile = $uploaddir . $code. $fileExtension); Link to comment https://forums.phpfreaks.com/topic/89120-renaming-file-upload/#findComment-456430 Share on other sites More sharing options...
Canman2005 Posted February 3, 2008 Author Share Posted February 3, 2008 for some reason it removes the file extension on your reply Thorpe, any idea why? Link to comment https://forums.phpfreaks.com/topic/89120-renaming-file-upload/#findComment-456464 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 $code = rand(111111, 999999); $uploaddir = 'fileupload/'; $ext = explode('.',basename($_FILES['upfile']['name'])); $uploadfile = $uploaddir . $code . '.' . $ext[1]; move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile); Link to comment https://forums.phpfreaks.com/topic/89120-renaming-file-upload/#findComment-456470 Share on other sites More sharing options...
Canman2005 Posted February 3, 2008 Author Share Posted February 3, 2008 thanks bud, worked a treat someone needs to put these simple scripts into an archive Link to comment https://forums.phpfreaks.com/topic/89120-renaming-file-upload/#findComment-456472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.