Hi all

i am a bit stuck lol
I am trying to get the file extention from a url say
yoursite.com/file.zip

in the data base the full link will be there, i am looking for a function that will take the full url
take ".zip" and then will output "(file type = ZIP )"

some help would be great

cheers

Recommended Answers

All 5 Replies

These functions seem to be along the line of what you're looking for:

  • strrpos
    Find the position of the last occurrence of a substring in a string
  • substr
    Return part of a string

So, you could use strpos to find where the last . character occurs in the string example.org/some_file.zip, and then use substr to get the part of the string that comes after that character (which would be zip in this case).

Hope that helps!

Hy
You can try with explode() function.
Example:

$str = 'yoursite.com/file.zip';
$ext = end(explode('.', $str));
echo 'file type = '. strtoupper($ext);     // file type = ZIP
Member Avatar for diafol

The function you need is pathinfo:

$bits = pathinfo('/path/file.zip');
echo $bits['extension'];

So for your case:

$bits = pathinfo($url);
echo "file type = " .strtoupper($bits['extension']);

ahh thanks that worked the trick

Member Avatar for diafol

We solved? If so, mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.