calvin182 Posted April 6, 2006 Share Posted April 6, 2006 I need to get the extention of a file, how would I do this. Link to comment Share on other sites More sharing options...
dcole.ath.cx Posted April 6, 2006 Share Posted April 6, 2006 get the URL or the address then take the last 4 charactorslike this:$file = "/folder/file.txt";$extension = substr($file, strlen($file) - 4);$extension will be .txt(with jpeg you will not have the dot) Link to comment Share on other sites More sharing options...
Little Goat Posted April 6, 2006 Share Posted April 6, 2006 that wont work though. ther are five letter file extensions such as .class which is a java applet file. maybe some way to get all the text after the last dot? Link to comment Share on other sites More sharing options...
calvin182 Posted April 6, 2006 Author Share Posted April 6, 2006 the only extentions i'll be needing this for are .bmp .gif .jpeg .jpg .png basically image types, so does anybody know how to get the stuff from the . and on? Link to comment Share on other sites More sharing options...
dcole.ath.cx Posted April 7, 2006 Share Posted April 7, 2006 FINE, were take the easy way out... I was trying to have fun! <?php$path_parts = pathinfo('/www/htdocs/index.html');print $path_parts['extension'];?> if you need more than the ending like: path?arg=value#anchorthen I would go with parse_url ( string url )this will output:Array( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor)but if you need from the dot on, you will have to break it at the . (dot) and take the $var[1] Link to comment Share on other sites More sharing options...
calvin182 Posted April 8, 2006 Author Share Posted April 8, 2006 thanks! that worked perfectly. Link to comment Share on other sites More sharing options...
Armed Rebel Posted April 9, 2006 Share Posted April 9, 2006 I know your problem is already solved, but this may be an easier way: $file_name="12345.abcd";preg_match('/\.[^.]+$/', $file_name, $ext); So the extension there would be ".abcd" and the Preg_match would send it out as an array, so use $ext[0]. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now