SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict aguroyz's Avatar
    Join Date
    Jan 2009
    Location
    Konoha Fire Country
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is it possible to check if a file or filename exists w/o knowing its extension??

    hello,

    Is it possible to check if a filename exists disregarding its extension?

    and also is their a way to retrieve its extension if the filename exists?

    can anyone help me..

    I tried using file_exists() function, but it returns false if I only specify the filename w/o the extension(.jpg)...
    Uniquely FILIPINO... See how talented and creative Filipinos are.
    http://www.smalltym.com
    Custom Web Designs:
    http://proweaver.com

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2009
    Location
    Bavaria, Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the only way to do this is to iterate over the directory. Have a look at the function readdir or the DirectoryIterator class. Something like this (untested):

    PHP Code:
    $yourfilename 'myfilename';
    if (
    $handle opendir('/your/directory')) {
        while (
    false !== ($file readdir($handle))) {
            
    $check substr($file0strlen($file)-4); // simplified... only for files with a three char extension
            
    if ($check == $yourfilename) {
                echo 
    'Yeah! Filename: '.$file;
            }
        }
        
    closedir($handle);


  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $exists count(new RegexIterator(new DirectoryIterator('somedir'), '/^' preg_quote($filename) . '\.[^.]+$/i')); 
    heh

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by crmalibu View Post
    PHP Code:
    $exists count(new RegexIterator(new DirectoryIterator('somedir'), '/^' preg_quote($filename) . '\.[^.]+$/i')); 
    heh
    I may be wrong but I don't think that will work as expected: even if $filename does not exist in the directory, the count will be 1 and $exists will always be TRUE. (If I am wrong, feel free to call me dumby from here on in )

    Neither DirectoryIterator nor RegexIterator implement the Countable interface so will not return how many items they hold when count() is used. You can however make use of the SPL function iterator_count which will count the number of items held by an iterator regardless of whether that iterator implements Countable or not.

    The amended line would be:
    PHP Code:
    $exists iterator_count(new RegexIterator(new DirectoryIterator('somedir'), '/^' preg_quote($filename) . '\.[^.]+$/i')); 
    We can also start globbing as an alternative to PCRE regular expressions:
    PHP Code:
    // GlobIterator available as of 5.3
    $exists = (bool) iterator_count(new GlobIterator("./somedir/$filename.*"));
    // glob available as of 4.3
    $exists = (bool) count(glob("./somedir/$filename.*")); 
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right, I assumed it implemented Countable.

    I purposely didn't use glob though, because searching for 'foo' would match foo.bar.jpg, although maybe that's desired or irrelevant.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You're right, I assumed it implemented Countable.
    As far as I'm aware (looking at the source code) only the GlobIterator does of that family of classes.

    I purposely didn't use glob though, because searching for 'foo' would match foo.bar.jpg, although maybe that's desired or irrelevant.
    A good point to make. The * character in a glob pattern really does mean anything (or nothing!) so foo.* would match foo.bar.baz.exe and foo. to give two examples; this may or may not be what the OP wants.

    Quote Originally Posted by aguroyz
    also is their a way to retrieve its extension if the filename exists?
    Well if you decided to use the Regex/Directory iterators approach, you could do something like:
    PHP Code:
    $dirname  "./somedir/";
    $filename "myfile";
    $iterator = new RegexIterator(new DirectoryIterator($dirname), '/^' preg_quote($filename) . '\.[^.]+$/iD');

    // Any matching files?
    if (iterator_count($iterator) > 0) {
        echo 
    "Matches found:\n";
    } else {
        echo 
    "No matches found.\n";
    }

    // Get extensions of matching files
    foreach ($iterator as $fileinfo) {
        
    printf("%s -> %s\n"$fileinfopathinfo($fileinfoPATHINFO_EXTENSION));

    Salathe
    Software Developer and PHP Manual Author.


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
  •