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 Link to comment https://forums.phpfreaks.com/topic/208794-scan-file-name-extension/ 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 Link to comment https://forums.phpfreaks.com/topic/208794-scan-file-name-extension/#findComment-1090772 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 Link to comment https://forums.phpfreaks.com/topic/208794-scan-file-name-extension/#findComment-1090774 Share on other sites More sharing options...
mraza Posted July 25, 2010 Author Share Posted July 25, 2010 Thank you Ken that looks perfect Link to comment https://forums.phpfreaks.com/topic/208794-scan-file-name-extension/#findComment-1090784 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.