Jump to content

scan file name extension


mraza

Recommended Posts

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

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

 

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

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.