SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File extension issue

    I am creating a file upload class and am finding a problem with most ways of finding the file extension of a file being uploaded.

    Most scripts have something similar to:
    PHP Code:
    function get_extension($filename)
    {
        
    $x explode('.'$filename);
        return 
    '.'.end($x);

    OR
    PHP Code:
    $path_parts pathinfo($filename);
    echo 
    $path_parts['extension']; 
    The problem is that does not recognize extensions like .tar.gz. Any ideas on how to get .tar.gz and other 3 or 4 character extensions to be found?

    I was thinking about just exploding the filename and using all but the first index in the array. Then join the other parts by a period but that would not work for something like: class.upload.php if you were allowing uploading of php files with naming like that. Since that would result in a file extension of .upload.php instead of .php.
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, an extension is everything after the *last* dot, by definition. So in your example myfile.tar.gz, the extension is .gz and the file name is myfile.tar. The fact that .tar can also be an extension doesn't make it an extension in this particular case.

    What's wrong with .gz?
    Saul

  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)
    .tar.gz = first tared then gzip;
    .gz is the file extension not .tar.gz.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, if for some reason you did want to get the tar part, then you would really need to compile a list of them to match against. However, there's no sure fire away of getting the "two" extensions and knowing it is an extension and not part of the file name without the extension.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There's no such way yes, simply because there are no second extensions -- there's only one. If you needed the .tar part, for example to extract files, you would need to analyse the filename; and for that you must recognize the .gz extension first, then act on it. But isn't it the same what you do with .gif or .jpg extenstions, as an example?
    Saul

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to compile a list of allowed file types anyway, not just allow users to indiscriminately upload any and all types. I strongly recommend trying not one but several MIME type checks before moving the uploaded file from /tmp into the publicly accessible space.

    PHP Code:
    // try fileinfo first
    if (extension_loaded('fileinfo') && (($finfo finfo_open(FILEINFO_MIME)) !== false)) {
    $mimeType finfo_file($finfo,$tempName);
    finfo_close($finfo);
    }
    // next try mime_magic
    else if (extension_loaded('mime_magic')) {
    $mimeType mime_content_type($tempName);
    }
    // as a LAST resort, take the browser's word for it
    else $mimeType $file['type'];
    // see if it's a type you want to allow
    $allowedTypes = array('image/gif','image/jpeg', ...);
    if (!
    in_array($mimeType,$allowedTypes)) die('Get out of here with that!'); 

  7. #7
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for your posts. Yes I am performing mime checks on the file before it gets moved to a public location.

    What the goal for finding the file extension was to return this information after successful file upload. Showing: filename, filename + upload path, file mime type, file size, and file extension.

    I will just return what is after the last . in the filename. Thanks
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers


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
  •