Results 1 to 6 of 6

Thread: How to determine a file's extenstion, etc.

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default How to determine a file's extenstion, etc.

    Every once in a while there is a question involving how to determine the characteristics of a file. I'm reading a book called Pro PHP patterns, frameworks, testing and more. And I just learned about this:

    PHP Code:
    <?php

    $file 
    '/path/to/file/file.ext';

    $pathInfo pathinfo($file);

    echo 
    'Extension: ' $pathInfo['extension'] . '</br />';

    echo 
    'Base name: ' $pathInfo['basename'] . '</br />';

    echo 
    'Directory name: ' $pathInfo['dirname'] . '</br />';

    echo 
    'File name: ' $pathInfo['filename'] . '</br />';

    ?>
    Pretty much lies to rest any discussion on how to determine a file's extension.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Or if you dont want an array, you could just do:
    Code:
    echo pathinfo('file.php', PATHINFO_EXTENSION);
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    What exactly is '</br />' supposed to be?

    Note that the extension of a file does not uniquely determine the contents of that file. Only the contents themselves do. For a more accurate attempt at guessing the intended interpretation of those contents, try mime_content_type() or, in PHP 5.3 or later, the Fileinfo module.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Twey View Post
    What exactly is '</br />' supposed to be?
    It's a typo. I bet you have even made a typo before. Should be <br />. And yes, I know you don't approve of XHTML.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, OK, I thought you copied it from the book

    Oh, I do approve of XHTML. If I ever made a purely personal site that I could be sure wasn't going to be used as part of my portfolio, I would use XHTML in a trice. In fact, I write all my templates in XHTML, and then have them converted on the fly to HTML4 for IE (the power of Genshi). For serious sites, though, most clients want to have IE support, and I hear it's considered unprofessional to throw away half your users
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Intriguing. Later today or tomorrow I am going to start a thread in the HTML form to talk more about this Twey. With your method, everyone can be happy.

    See ya,

    J

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •