I'm writing the code for a website right now that's uploads images and then displays them gallery style. What I would like to happen is for the file name of the image to be entered into the site's database as the name of the image. However, just using $_FILES['images']['name'];
gives me the file name but with the file extension attached at the end. How would I remove the file extension so I can use the file name by itself?
Asked
Active
Viewed 4,002 times
3

codedude
- 6,244
- 14
- 63
- 99
-
2what part of the name do you consider the extension? everything before the first dot? what about `some.name.tar.zip` which is a valid name. is `.zip` the extension and `some.name.tar` the name or is `some` the name and `name.tar.zip` the extension? In reality, you should include the extension as part of the name so when you how someone the name, they can identify the file type. – Jonathan Kuhn Oct 06 '11 at 19:45
4 Answers
0
Just use preg_replace:
$name = preg_replace('/(.+)\.\w+$/U', $_FILES['images']['name'], '$1');

Aleks G
- 56,435
- 29
- 168
- 265
0
As my comment above implies, it depends on what you consider in the filename to be the name and what is the extension.
everything up to the last dot:
$filename = 'some.file.name.zip';
$name = substr($filename, 0, strrpos($filename, '.'));
everything up to the first dot:
$filename = 'some.file.name.zip';
$name = substr($filename, 0, strpos($filename, '.'));
they look the same, but the first one looks for the first dot from the end of the string and the second one from the start of the string.

Jonathan Kuhn
- 15,279
- 3
- 32
- 43