SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if file exists without extension.

    Hi,
    I was just wondering how I could check if a file exists without the extension, and then get the extension? Basically "filename" could be .jpg .png or .gif but I won't know which. Any help would be appreciated.

    Cheers,
    Rhys

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    I'll give you 2 hints as to how i'd do it, and lets see if you can figure it out

    scandir
    preg_grep

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    *cough* glob */cough*
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Which is fine if you want to scan the entirity of your structure. If you want a specific directory scanned, you'll need a bit more refinement than glob

  5. #5
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    scandir and create an array of files, then check for the filename using preg_grep?

    Something along the lines of:
    PHP Code:
    $dir    '/images/montage';
    $files scandir($dir);
    $matches preg_grep($filename.??EXT??, $files);
    echo 
    $matches[0]; 
    Is that right? And how would I know what I have, ie .jpg?

    Thanks for the replies.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    preg_grep for "~".$filename."~" (need the delimiters on the pattern)

    foreach ($matches AS $match) {
    echo end(explode('.',$match));
    }

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Which is fine if you want to scan the entirity of your structure. If you want a specific directory scanned, you'll need a bit more refinement than glob
    Can you elaborate on "scan the entirity of your structure"?
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I was wondering the same thing. Surely glob, a system function acting on the CWD, is faster than grabbing all files in a directory and separately applying a regular expression?

    Why not just:
    PHP Code:
    $dir 'images/montage';
    $filename 'test';
    $files glob("{$dir}/{$filename}.*");
    var_dump($files); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TacMaf View Post
    Hi,
    I was just wondering how I could check if a file exists without the extension, and then get the extension? Basically "filename" could be .jpg .png or .gif but I won't know which. Any help would be appreciated.

    Cheers,
    Rhys
    In addition to all the functions mentioned above, you can use pathinfo to break the path up into sections, including getting the extension of the file or getting its base name.

    Wait, ok - glob allows you to use a pattern, so you just need the pattern for your search. I think this will work - let me know if it works...

    PHP Code:
    $imageFilesInDirectory glob('*.{jpg}{png}{gif}{jpeg}'); 
    Do NOT use double quotes on that pattern, as the braces would be interpretted by PHP as a variable delimit. In that string the braces delimit the pattern of the extension being sought.

    If that works as I think it will return the image files of the current working directory. If you know the directory you need to search, prepend it by absolute path like so...

    PHP Code:
    $imageFilesInDirectory glob($dir.'/*.{jpg}{png}{gif}{jpeg}');  // Shouldn't there need to be some | operator between extensions sought? 

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    I was wondering the same thing. Surely glob, a system function acting on the CWD, is faster than grabbing all files in a directory and separately applying a regular expression?
    I have 20,000 files/images in a subdirectory of my site (I'm running a Wiki.)
    In another subdirectory, I have 2 files.

    You're telling me glob'ing across 20002 files is going to be faster than scandir'ing 2 and running a preg across them?

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Touche good sir. Dare I challenge you to a duel with some chdir or shall I just accept defeat?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    I'd be interested to see what's quicker:

    chdir,glob,chdir (because you gotta get back to the original dir)
    or
    scandir,preg_grep

    Honestly, I dont know which would be faster.

  13. #13
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    I'm completely lost, could you answer my earlier question, StarLion? What's with the chdir()-ing? Also, how is glob()-ing a folder (or several) and different to scandir()-ing a folder (or several)? glob is essentially scandir+regex*. I don't understand how you think one would do something 20,002 times and the other only 2.

    * wildcard pattern matching, not regex.


    Quote Originally Posted by TacMaf View Post
    Hi,
    I was just wondering how I could check if a file exists without the extension, and then get the extension? Basically "filename" could be .jpg .png or .gif but I won't know which.
    As the others have noted, one way would be to use glob() to look for files matching a pattern.

    PHP Code:
    $files glob('path/to/files/filename.{jpg,png,gif}'GLOB_BRACE); 
    Another option is just to loop over the extensions until you find one, or not.

    PHP Code:
    $extensions = array('jpg''png''gif');
    foreach (
    $extensions as $ext) {
        if (
    file_exists('path/to/files/filename.' $ext)) {
            echo 
    'Found filename.' $ext;
            break;
        }

    Salathe
    Software Developer and PHP Manual Author.

  14. #14
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Off Topic:

    I'm completely lost, could you answer my earlier question, StarLion? What's with the chdir()-ing? Also, how is glob()-ing a folder (or several) and different to scandir()-ing a folder (or several)? glob is essentially scandir+regex*. I don't understand how you think one would do something 20,002 times and the other only 2.

    * wildcard pattern matching, not regex.
    Unless I'm very confused about the method of glob, running glob() pulls an entry for every file of every directory at-or-below the CWD, checks it against the wildcard, and then either returns it or doesnt, and moves on.

    In the example above, if you stayed in the root directory and glob'd, glob would have to work over 20002 files (plus however many were in the root directory itself).

    Jake's statement about chdir moves the CWD to the subdirectory first, so glob() only has to run over 2 files. chdir would be needed to move back to the inital directory at the end to prevent issues with other file operations in the page execution.

    Scandir takes the directory to scan as a parameter, and so will only scan the 2 files without needing to change the CWD. (Well, 4 files on a *NIX system, as . and .. will be returned as well, but the point remains.)

  15. #15
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure where the apparent need for calling chdir() came from, that's not the case at all. With glob() you can provide any path you like (including absolute paths) as part of the pattern, as in my example above.

    As for glob() looking recursively into all files/folders from the specified path, that's not the case at all. Glob can be told to look in multiple folders, given the right pattern, e.g. path/to/folders/*/*.txt. The most usual case (again see my example) is to look at the contents of a single folder (by specifying a path with no wildcards, path/to/files/) for file/folder names matching a wildcard pattern (filename.*).
    Salathe
    Software Developer and PHP Manual Author.

  16. #16
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    So glob walks to a directory first? Handy to know. Thanks Salathe!

    (should probably say "it sub patterns through the directories first", but it's 9am and I'm on an iPhone. Editing is hard)

  17. #17
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow a lot of responses. Thanks. I wasn't in work Friday so only just seen them. I think I will give glob a try:
    PHP Code:
    $files glob('path/to/files/filename.{jpg,png,gif}'GLOB_BRACE); 
    So thanks for that and for all help.

    I will give it a try first thing when I'm back in and post results. Should be much cleaner than the old system of grabbing the filenames from the DB.

    Cheers,
    Rhys

  18. #18
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slight problem. Just trying to debug and make sure some files are being returned. I have:
    PHP Code:
    $files glob(URL_IMG.'montage/montage-'.$categoryID.'.{jpg,png,gif,jpeg}'GLOB_BRACE);
    foreach (
    $files as $filename){
    echo 
    $filename."\n";

    Which returns Warning: Invalid argument supplied for foreach() in ....

    I thought $files would be an array and that code would be OK?

  19. #19
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK it turns out glob() simply isn't finding anything. Even with a wildcard extension. Nothing is retuned. Any ideas why glob() wouldn't work?

  20. #20
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    What is the value of URL_IMG.'montage/montage-'.$categoryID.'.{jpg,png,gif,jpeg}'?
    Salathe
    Software Developer and PHP Manual Author.

  21. #21
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I echo'd it and got http://www.domain.co.uk/images/montage/montage-2.{jpg,png,gif,jpeg}

  22. #22
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see your point already. I've changed it to the directory rather than url and it works! thanks!

  23. #23
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Good work.
    Salathe
    Software Developer and PHP Manual Author.

  24. #24
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But how would I now echo it as an image to the user now? At current I have:
    PHP Code:
    if(is_array($files) && count($files 0)){
    return 
    '<img alt="Montage" src="'.$files[0].'" />';

    Which returns image location as g:\websites\thissite\htdocs\images\montage\montage-4.png

    Shows it exists but no good for the user. Shouldn't glob just return the filename?

  25. #25
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TacMaf View Post
    Shows it exists but no good for the user. Shouldn't glob just return the filename?
    No, it shouldn't. This is because glob() can return files from multiple folders (given an appropriate pattern). You could str_replace() the filesystem path with the URL path, or use basename() with the path, it's up to you.
    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
  •