frobak Posted February 8, 2012 Share Posted February 8, 2012 Hi I need to change the name of a file being uploaded by a user. The reason i need this is because there is a strong possibility that duplicate filenames would be logged. This is the code i have currently: $upload_path = 'cv/'; // The place the files will be uploaded to (currently a 'files' directory). $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension). $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. // Check if we can upload to the specified path, if not DIE and inform the user. if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); // Upload the file to your specified path. if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)); This code works fine to upload the file in the current name. I assume i need to seperate the filename from the file extension, and i can then assign a new variable to the filename. Easier said than done though as Ive tried many combinations of things. Is there a simple way using this script? or will i need to start from scratch? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/ Share on other sites More sharing options...
SergeiSS Posted February 8, 2012 Share Posted February 8, 2012 What is the main problem? To create a new filename in an easy name? You may use pathinfo() function http://ru.php.net/manual/en/function.pathinfo.php in order to get all filename parts. Then you may change something and create a new filename. Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1315920 Share on other sites More sharing options...
frobak Posted February 8, 2012 Author Share Posted February 8, 2012 Hi Yeah the problem is that i need to change the filename of the uploaded file, not the extension, just the filename. or even just append a user id to the beggining of the filename. CV's are being uploaded by users, so i would need to chnage the fielname or I could have many duplicate filenames, people uploading cv.doc for example. pathinfo() doesnt split the extension? Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1315934 Share on other sites More sharing options...
PaulRyan Posted February 8, 2012 Share Posted February 8, 2012 Something like this will work. <?PHP //### The place the files will be uploaded to (currently a 'files' directory). $upload_path = 'cv/'; //### Get the name of the file (including file extension). $filename = $_FILES['userfile']['name']; //### Explode the periods $explode = explode('.',$filename); //### Get extension of the filename $ext = $explode[(count($explode)-1)]; //### Create a new filename - Do what you like here $newFilename = md5(microtime(true)); //### Merge the file name, with the file extension $filename = $newFilename.'.'.$ext; //### Check if we can upload to the specified path, if not DIE and inform the user. if(!is_writable($upload_path)) { die('You cannot upload to the specified directory, please CHMOD it to 777.'); } //### Upload the file to your specified path. if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { echo 'File was moved'; } else { echo 'File was not moved.'; } ?> Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1315943 Share on other sites More sharing options...
frobak Posted February 8, 2012 Author Share Posted February 8, 2012 Paul, that worked like a charm, Cheers Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1315971 Share on other sites More sharing options...
The Little Guy Posted February 8, 2012 Share Posted February 8, 2012 instead of doing all that exploding, you can do this: $info = pathinfo("/path/to/file.jpg"); $new_name = md5(time()); $filename = $info['dirname']."/".$new_name.".".$info['extension']; move_uploaded_file($_FILES['userfile']['tmp_name'], $filename); Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1315975 Share on other sites More sharing options...
PaulRyan Posted February 8, 2012 Share Posted February 8, 2012 I totally forgot about pathinfo(), nice example Little Guy. Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1315989 Share on other sites More sharing options...
SergeiSS Posted February 9, 2012 Share Posted February 9, 2012 I totally forgot about pathinfo()... Really? Maybe you are just absent-minded? The first recommendation in this topic was to use exactly this function - pathinfo(). Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1316040 Share on other sites More sharing options...
PaulRyan Posted February 9, 2012 Share Posted February 9, 2012 Yeah, thanks for pointing that out, I tend to skip replies that don't contain any code, I don't know why, I just do Quote Link to comment https://forums.phpfreaks.com/topic/256693-change-the-name-of-uploaded-file/#findComment-1316114 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.