11

Is there any method of obtaining the name of the file uploaded to a server without extension, in PHP? I used the $_FILES['file']['name'] but it returns the extension too.

rid
  • 61,078
  • 31
  • 152
  • 193
chetan
  • 1,385
  • 4
  • 15
  • 31

6 Answers6

47
$filename = pathinfo($_FILES['file']['name'], PATHINFO_FILENAME);

pathinfo is a core PHP function since 4.0.3, and the PATHINFO_FILENAME option was added in 5.2.

Marc B
  • 356,200
  • 43
  • 426
  • 500
3

Use PHP basename() function.

string basename ( string $path [, string $suffix ] )
Riz
  • 9,703
  • 8
  • 38
  • 54
1

Use pathinfo().

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

Yes, you can do it as:

$filenameOnly = array_pop(array_reverse(explode(".", $yourfilename)));

Hope that helps

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1
preg_replace('^(.*)\..+?$', $_FILES['file']['name']);
rid
  • 61,078
  • 31
  • 152
  • 193
1

Try basename or use a regex to do it indiscriminately:

preg_replace('/(.*)\\.[^\\.]*/', '$1', $filename);
Godwin
  • 9,739
  • 6
  • 40
  • 58