SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    substr to get file extension

    I have the file and extension stored in a database and would like to know if there's something out there like substr but only backwards or even strchr but backwards? I want to get the extension of a file and right now i'm doing this
    Code:
    If (strchr ( $Row["File"], "." ) == ".pdf")
    but if someone uploads a file with .'s in it then it doesn't work.

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only thing I can think of is doing:
    PHP Code:
    function getExt($file) {
        
    $tmp explode("."$file);
        return 
    $tmp[(count($tmp) - 1)];
    }
    if (
    getExt($row['file']) == "pdf") {

    - the lid is off the maple syrup again!

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    strrchr
    http://www.php.net/manual/en/function.strrchr.php

    Only thing I can think of is doing:
    [php]
    function getExt($file) {
    $tmp = explode(".", $file);
    return $tmp[(count($tmp) - 1)];
    }
    if (getExt($row['file']) == "pdf") {
    }
    PHP Code:
    $ext end(explode('.'$file)); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (substr($Row["File"],-4,4) == ".pdf"
    or pathinfo

    Edit:

    this place is crowded
    Saul

  5. #5
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would have said pathinfo() but you can't do that, can you? Since the filename is a string returned from the database...
    - the lid is off the maple syrup again!

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    And why not? What difference does it make where does it come from?
    Saul

  7. #7
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just thought the path of the file any everything had to be there to do it. Learn something new every day.
    - the lid is off the maple syrup again!

  8. #8
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $ext = end(explode('.', $file));
    works beautifully, thanks

  9. #9
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DKY View Post
    $ext = end(explode('.', $file));
    works beautifully, thanks

    What if the file is my.little.file.txt ?

    It's a valid file, but you won't get the extension. Either validate that the file is in [A-Za-z0-9] or use the earlier methods in this post. The explode method will work, it's just not bullet proof.
    Chrispian H. Burks
    Nothing To Say

  10. #10
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to use substr(), use this code:
    PHP Code:
    function getFileExt($file
        {
            
    $i 0;
            
    $ext '';
            while ((
    $pos strpos($file'.'$i)) !== false) {
                
    $ext substr ($file$pos 1);
                
    $i++;
            } 
            return 
    $ext;
        } 

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $a = pathinfo('file/with/path/filename.tar.gz');
    $b = pathinfo('just_filename.tar.gz');
    echo $a['extension']; // result is "gz"
    echo $b['extension']; // result is "gz"

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Crowe View Post
    What if the file is my.little.file.txt ?

    It's a valid file, but you won't get the extension. Either validate that the file is in [A-Za-z0-9] or use the earlier methods in this post. The explode method will work, it's just not bullet proof.
    If the file is "my.little.file.txt" #ext = end(explode('.', 'my.little.file.txt'); will return "txt" because it will be the end of an array.

    Also using pathinfo is recommend, I;m just defending the method i previously told which does fully work.

    PHP Code:
    $ext pathinfo($filePATHINFO_EXTENSION); // PHP 5.2 Syntax 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If you are not on PHP 5 but would like to use its syntax

    PHP Code:
    if (!defined('PATHINFO_DIRNAME')) {
        
    define('PATHINFO_DIRNAME'1);
    }

    if (!
    defined('PATHINFO_BASENAME')) {
        
    define('PATHINFO_BASENAME'2);
    }

    if (!
    defined('PATHINFO_EXTENSION')) {
        
    define('PATHINFO_EXTENSION'4);
    }

    if (!
    defined('PATHINFO_FILENAME')) {
        
    define('PATHINFO_FILENAME'8);
    }

    function 
    ppathinfo($path$option null) {
        
    $path pathinfo($path);

        switch (
    $option) {
            case 
    PATHINFO_DIRNAME:
                return 
    $path['dirname'];
                break;
            case 
    PATHINFO_BASENAME:
                return 
    $path['basename'];
                break;
            case 
    PATHINFO_EXTENSION:
                return 
    $path['extension'];
                break;
            case 
    PATHINFO_FILENAME:
                return 
    $path['filename'];
                break;
            default:
                return 
    $path;
                break;
        }

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    If the file is "my.little.file.txt" #ext = end(explode('.', 'my.little.file.txt'); will return "txt" because it will be the end of an array.

    Also using pathinfo is recommend, I;m just defending the method i previously told which does fully work.

    PHP Code:
    $ext pathinfo($filePATHINFO_EXTENSION); // PHP 5.2 Syntax 
    You're right. I didn't catch the "end" part in your code!
    Chrispian H. Burks
    Nothing To Say

  15. #15
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try for this code

    $file_extension = strtolower(substr(strrchr($file,"."),1));
    And let me know
    Barbara

  16. #16
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DKY View Post
    Code:
    If (strchr ( $Row["File"], "." ) == ".pdf")
    PHP Code:
    if(preg_match('/\.pdf$/D'$Row["File"]))... 


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
  •