Hi guys,
I'm having some problem creating a php function
that searches a specific path for for files that match the patern
"photos*.jpg" or "pics_?.gif". If the files are found I would like
the results to be returned to an array.
Here is the break down
BTW. I cannot use the glob because the server is running PHP 4.2.2
I currently working on a realestate website and I need to check if a photo(s)
exists for a property (house, busnisess, etc..). The photo name coresponds to the propety id. for example if the property id
number = 50123, then the filename for that photo would be "50123.jpg". However there are some properties that have more than one photos so for the secon photo the filename would be something like "50123_2.jpg" and the 3rd would be "50123_3.jpg" and so on.
so now, given the directory with the images I have to check if a photo exist for
a selected property and also I need to check if there are more images for that property.
Thanks
Find files (photos*.jpg) - (req: glob replacement)
Moderator: General Moderators
I gave this a go without any testing:
Try it out, and if it don't work, let me know.
Code: Select all
function PropertyImageSearch ($dir)
{
if (!is_dir($dir)) return array();
$properties = array();
$dh = opendir($dir);
while( ($file = readdir($dh)) !== false)
{
if (preg_match('/^([0-9])+(_[0-9]+)?\.(jpg|jpeg|gif)$/i', $file, $matches))
{
$properties[$matches[1]][] = $file;
}
}
return $properties;
}
$prop_images = PropertyImageSearch('/home/images/houses');
var_dump($prop_images);my mother asked me to make something for her to be able to upload pics to directories on a server i set up and then give the url to friends...the server runs 4.2.2.
this is taken directly from it and i know works. i hope this is commented well enough that you actually understand what's happening instead of just taking my code
this is taken directly from it and i know works.
Code: Select all
# set the directory
$dir = $_GET['dir']; # the directory (will become the gallery)
$page=$_GET['page']; # the page number of the gallery
$jpgs=array(); # array for jpgs
$pngs=array(); # array for pngs
$gifs=array(); # array for gifs
# get the contents
$d=opendir($dir) or die($php_errormsg);
while(false !==($f=readdir($d))){
$here=getcwd(); $pospics=$here . '/' . $dir . $f; # create a absolute path to the pic
if(is_file($pospics)){ # if it's a file
if((preg_match('/\.jpg$/', $f))||(preg_match('/\.jpe$/', $f))||(preg_match('/\.jpeg$/', $f))){ # with a jpeg extension
$jpgs[] = $f; # add to jpegs
}elseif(preg_match('/\.png$/', $f)){ # with png extension
$pngs[] = $f; # add to pngs
}elseif(preg_match('/\.gif$/', $f)){ # gif extension
$gifs[] = $f; # add to gifs
}
}
}
# sort the arrays for later use
sort($jpgs);
sort($pngs);
sort($gifs);Thanks, but
Hi Guys,
Thanks for your help, but I'm not try to retrieve a list of all the in the
directory, I'm trying to find the photos that belong to a specific record id.
The photo name coresponds to the propety id. for example if the property id
number = 50123, then the filename for that photo would be "50123.jpg". However there are some properties that have more than one photos so for the secon photo the filename would be something like "50123_2.jpg" and the 3rd would be "50123_3.jpg" and so on.
Thanks!
Thanks for your help, but I'm not try to retrieve a list of all the in the
directory, I'm trying to find the photos that belong to a specific record id.
The photo name coresponds to the propety id. for example if the property id
number = 50123, then the filename for that photo would be "50123.jpg". However there are some properties that have more than one photos so for the secon photo the filename would be something like "50123_2.jpg" and the 3rd would be "50123_3.jpg" and so on.
Thanks!
assuming you have the id passed to search, then use what i havem but adjust it to :
preg_match('/$id[_\d+]?\.jpg$/', $f) and so on.
incase you don't know perl,
' are just to make stringes, and / signify the begining and end of the match string
$id is to signify the id passed from php.. you'll have to talk to someone with more experience to find out how exactly to do that
[_\d+]? means that there's a possible underscore followed by one or more numbers. this part is optional.
\.jpg$ means it must end with \.jpg$
preg_match('/$id[_\d+]?\.jpg$/', $f) and so on.
incase you don't know perl,
' are just to make stringes, and / signify the begining and end of the match string
$id is to signify the id passed from php.. you'll have to talk to someone with more experience to find out how exactly to do that
[_\d+]? means that there's a possible underscore followed by one or more numbers. this part is optional.
\.jpg$ means it must end with \.jpg$