Results 1 to 7 of 7

Thread: finding a file...

  1. #1
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default finding a file...

    Hello, I need to find a file in a particular directory. The file will have a static name (say, "image") but may be in either jpg, gif, or png format.

    I've tried lots of variations on glob() , but they all return empty arrays. Yes, the file ( image.jpg ) does exist in the specified directory.

    PHP Code:
    $file glob($_SERVER['DOCUMENT_ROOT'].'/images/image.{gif,jpg,png}'GLOB_BRACE); 
    Any help would be appreciated! thanks.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Using the following should be better. Then, if you need the absolute location, use realpath.
    PHP Code:
    $file glob('images/image.{gif,jpg,png}'GLOB_BRACE); 

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    thanks, Tim
    it works when glob() is in the directory below images/ , but I can't get it to work from a more complicated relative location. For example, the image is in:
    Code:
    www.example.com/images/image.jpg
    , but the page is at:
    Code:
    www.example.com/pages/page.php
    . In the interests of portability, I'd rather not force the user into a specific directory arrangement by using ../images/image.jpg, since the page might someday be at (e.g.) /pages/otherpages/page2.php...

    The class this is in has a default path to the images folder (which is the path I'm using now), but I need the class to be able to find the image from wherever it's executing, which is why I was trying to get a server-relative path to work. This:
    PHP Code:
    $globpath realpath($this->defaultImagePath);
    $file glob($globpath.'image.{gif,jpg,png}'); 
    finds nothing. GLOB_NOCHECK returns only image.{gif,jpg,png}.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I do not know the answer to your problem, but I am curious as to the nature of it. Are you saying you want to find a file named image, but the extension is unknown and the directory it is in is unknown and you want the program to find the file no matter which subdirectory of many the search program is located in relative to the file it is looking for or is there more to your problem?

    I want to add that I am unfamiliar with glob(), but I did look it up briefly at php.net. I take it using opendir() and putting the files into an array for searching purposes and if one of the files in the directory is a directory to open it and search it as well would not work in your situation?
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    actually, here's the entire situation (I actually solved this problem another way, but I'm convinced it should work and I can't figure out why it doesn't).

    This is in a class. The image in question is going to be replaced frequently, but will always be given the same name. There may be other images in the directory, but I'm only interested in the one for this purpose. The image type, however -and therefore, the extension- may vary. In part:
    PHP Code:
    //most code omitted for this example.
    class photo{

    private 
    $image_path;
    private 
    $image_filename;

    public function 
    __construct($custom_path=NULL){
          
    // use the user-defined image directory path if present, otherwise
          // default path is {docroot}/photoclass/images/
        
    $this->image_path = empty($custom_path) ? '/photoclass/images/' $custom_path;
          
    // I already know the path, so use glob() to determine image type
        
    $find glob($this->image_path.'filename.{gif,jpg,png}'GLOB_BRACE);
          
    // *should* be only one match; use first result. need some error checking, I suppose.
        
    $find $find[0];
        
    $this->image_filename $find;

        
    // then other stuff happens
    }


    I ended up working around this by using:
    PHP Code:
    $exts = array('gif','jpg','png');
    foreach(
    $exts as $ex){ 
        if(
    file_exists($this->image_path.'filename.'.$ex)){ 
            
    $this->image_filename 'filename.'.$ex
        } 

    it'll work fine for this application, but I want to get glob() working too. I'd be quite disappointed if I can only look through directories I'm sitting underneath.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Not that this answers your original question either, but you can always use a loop like this:
    PHP Code:
    <?php
    $d 
    opendir($PATH);
    while (
    $f readdir($d)) {
       if (
    MATCH) {
           
    //do whatever
       
    }
    }
    ?>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Yup. Maybe Techietim will have another suggestion. It was a post of his a few months ago that led me to this fascination with glob() !

    So, in short, the original problem is solved.

    I am still interested in getting root-relative paths to work with glob() and in learning more about how to format/escape/prepare (whatever) variables to be inserted into the function.

    Thanks, guys!

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
  •