View Full Version : 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.
$file = glob($_SERVER['DOCUMENT_ROOT'].'/images/image.{gif,jpg,png}', GLOB_BRACE);
Any help would be appreciated! thanks.
techietim
11-22-2009, 01:20 PM
Using the following should be better. Then, if you need the absolute location, use realpath.
$file = glob('images/image.{gif,jpg,png}', GLOB_BRACE);
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:
www.example.com/images/image.jpg, but the page is at:
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:
$globpath = realpath($this->defaultImagePath);
$file = glob($globpath.'image.{gif,jpg,png}');
finds nothing. GLOB_NOCHECK returns only image.{gif,jpg,png}.
james438
11-23-2009, 01:04 AM
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?
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:
//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:
$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.
djr33
11-23-2009, 05:40 AM
Not that this answers your original question either, but you can always use a loop like this:
<?php
$d = opendir($PATH);
while ($f = readdir($d)) {
if (MATCH) {
//do whatever
}
}
?>
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() ! :D
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!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.