mraza Posted July 24, 2010 Share Posted July 24, 2010 Hi i wants to know how can i get a file name extension in directory, for example file name is "thisfile" and there could be any of these thisfile.txt or thisfile.pdf or thisfile.doc in current directory , but there is only one file with that name in directory either with txt or pdf or doc extension, and i need to get that file name extension , please can someone help how can i know the extension of "thisfile" using php. thanks Quote Link to comment Share on other sites More sharing options...
mraza Posted July 24, 2010 Author Share Posted July 24, 2010 i dont know if i was good but i make it work with it, function FileExtension($filename) { $mp4 = "mp4"; $flv ="flv"; $avi = "avi"; $gp = "3gp"; $mpeg = "mpeg"; $flvcheck = $filename.".".$flv; $mp4check = $filename.".".$mp4; $avicheck = $filename.".".$avi; $gpcheck = $filename.".".$gp; $mpegcheck = $filename.".".$mpeg; echo $test; if (file_exists($flvcheck)) { $extension = $flv; } elseif (file_exists($mp4check)) { $extension = $mp4; } elseif (file_exists($avicheck)) { $extension = $avi; } elseif (file_exists($mpegcheck)) { $extension = $mpeg; } else { $extension = $flv; } echo $extension; } If there was some more fency way plz let me know Thx Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 24, 2010 Share Posted July 24, 2010 Try something like this: <?php function FileExtension($filename) { $valid_extensions = array('mp4','flv','avi','3gp','mpeg'); $file_found = false; $ret = ''; foreach ($valid_extensions as $ext) { if (!$file_found && file_exists("$filename.$ext")) { $ret = $ext; $file_found = true; } } return ($ret); } ?> Ken Quote Link to comment Share on other sites More sharing options...
mraza Posted July 25, 2010 Author Share Posted July 25, 2010 Thank you Ken that looks perfect Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.