Jump to content

Getting the extension of a file


glenelkins

Recommended Posts

[!--quoteo(post=350185:date=Feb 28 2006, 07:17 AM:name=glenelkins)--][div class=\'quotetop\']QUOTE(glenelkins @ Feb 28 2006, 07:17 AM) [snapback]350185[/snapback][/div][div class=\'quotemain\'][!--quotec--]
what function is there to test the extension of a file?
[/quote]

you can write one:
[code]
<?php
function getExt($filename) {
  $pos = strrpos($filename, '.');
  if ($pos !== false) {
    return substr($filename, $pos + 1);
  } else
    return NULL;
}
?>
[/code]
Look at the [a href=\"http://www.php.net/pathinfo\" target=\"_blank\"]pathinfo[/a]() function.

Example from the manual:
[code]<?php
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
?> [/code]

Ken
[!--quoteo(post=350239:date=Feb 28 2006, 10:34 AM:name=glenelkins)--][div class=\'quotetop\']QUOTE(glenelkins @ Feb 28 2006, 10:34 AM) [snapback]350239[/snapback][/div][div class=\'quotemain\'][!--quotec--]
in the other example (linked site) they use a function called: strch() but that returns an undefined error wierd
[/quote]

they are trying to use strchr(), but it's simply an alias of strstr() anyway

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.