I’m using PHP to upload an image from a form to the server and want to rename the image lastname_firstname.[original extension]. I currently have:
move_uploaded_file($_FILES["picture"]["tmp_name"], "peopleimages/" . "$_POST[lastname]" . '_' . "$_POST[firstname]")
which, of course, renames the file lastname_firstname without an extension. How do I rename the file but keep the extension?
Thanks!
You need to first find out what the original extension was 😉
To do that, the
pathinfofunction can do wonders 😉Quoting the example that’s given in the manual :
Will give you :
As a sidenote, don’t forget about security :
$_POST[lastname], to make sure it only contains valid characters$_POST['lastname']— see Why is$foo[bar]wrong?mime_content_typefor PHP < 5.3finfo_filefor PHP >= 5.3