FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Urgent - Retrieving File Extension From A String.





cr3ativ3
Hey, I am have huge problems with this simple problem. I am trying to retrieve the file extension of a file from a string like I have string say "file.php" how do I get the .php out of that with out just saying it is .php like:

Say I have a bunch of files that I might be using in actuallity it is infinite.

So lets say the files it could be for now are:

file.php
file.htm
file.html
file.gif
file.jpg
file.css
file.js

If I don't know what the file extension is and I can't think of possible extensions. What would I do to retrieve the file extension on those.

I'm thinking of maybe some function that takes the string and cuts off the first section of the string before the dot. Rather then cutting of the extension like strtok($value, ".");.

Also you should note my host does not have finfo functions or there is one other function for gathering the mimetypes forgot what it was and can't find it now though.

Thanks!
rohan2kool
here's a function:

Code:

function get_file_ext($fname, $nodot = true) {
   $offset = -1;
   $len = strlen($fname); //Use a variable -- minor reduction in gentime

   for($i = $len; $i >= 0; $i--) {
      if(substr($fname, $i, 1) == '.') {
         $offset = $i;
         break;
      }
   }

   if($nodot == false) {
      return (substr($fname, $offset));
   } else {
      return (substr($fname, ($offset + 1)));
   }
}


Use it like: get_file_ext($fname, $nodot);

where, $fname is the string containing the name of the file and $nodot is a boolean. Setting it to 'true' will return the extension without a dot. Default behaviour is returning without a dot.

for ex:

get_file_ext("file.txt", true); //returns: txt
get_file_ext("file.txt", false); //returns: .txt
get_file_ext("file.txt"); //returns: txt
cr3ativ3
Thank you for the swift response.
cr3ativ3
Okay, I tried using but when I include the .php file I have the function in. It seems to echo out the whole script to the browser and when I use the function I get this error:

Quote:

Fatal error: Cannot redeclare get_file_ext() (previously declared in /home/oymaster/public_html/filem.php:118) in /home/oymaster/public_html/filem.php on line 118


So in total it looks like this:
Quote:

function get_file_ext($fname, $nodot = true) { $offset = -1; $len = strlen($fname); //Use a variable -- minor reduction in gentime for($i = $len; $i >= 0; $i--) { if(substr($fname, $i, 1) == '.') { $offset = $i; break; } } if($nodot == false) { return (substr($fname, $offset)); } else { return (substr($fname, ($offset + 1))); } }

Fatal error: Cannot redeclare get_file_ext() (previously declared in /home/oymaster/public_html/filem.php:118) in /home/oymaster/public_html/filem.php on line 118
cr3ativ3
K nvm I spent some time searching google and I was able to find this script and it works perfectly. I'll share it with you everyone.

Code:

function file_extension($filename)
{
    $path_info = pathinfo($filename);
    return $path_info['extension'];
}


So thats just,
$ext = file_extension($file);
rohan2kool
basically.. in the first problem, you just need to change the name of the function (most probably a function with that name is already declared in some other file, which you have included).. just change it to something like getFileExtension() or something that has not been previously declared, but since you know have a way out already.. Very Happy
cr3ativ3
Oh, ok well ya this other function is built in and takes less code so I am just using this for now but ya thanks for the other script. Very Happy
Related topics
req: PHP to list files in directory, and link to them
.htaccess tutorial
Windows Tips&tricks!
File unable to be downloaded
replace a string for a position of text
How to detect a file is that jpeg image or gif image ?
[php] Php Editor
What file extension should I use ?
How to prepare a true "GOOGLE XML" file - SITEMAP.
Windows shortcuts, must know
Search my sites folders for a certain file extension
Hide PHP file extension
file extension SYS
Interesting mistake - change extension on image
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.