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
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

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.