SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 19th, 2003, 08:24 PM
omar omar is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 18 omar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question php - Find files (photos*.jpg) - return results to an array

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

Thanks!.

Reply With Quote
  #2  
Old June 19th, 2003, 11:23 PM
AmericanD AmericanD is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 81 AmericanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Why dont u tell us what you already tried? Its easier and better to help you fix something you did rather than do something for you from scratch for FREE
__________________
Hungry for Code

Programming works best with a team over one single person

Reply With Quote
  #3  
Old June 19th, 2003, 11:31 PM
AmericanD AmericanD is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 81 AmericanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
This might be helpful

If it's the mime-type you are after, you could parse the mime.types file (in *NIX):

$file = file('/etc/mime.types');
foreach($file as $line) {
rtrim($line);
if (preg_match('/^\#/', $line))
continue;
$elms = preg_split('/\s+/', $line);
$type = array_shift($elms);
foreach ($elms as $elm) {
$mime[$elm] = $type;
}
}

Example: $mime[pdf] will return "application/pdf" (or whatever your mime.types file contains).



And/Or this


function get_file_type($filename) {
ereg( ".*\.([a-zA-z0-9]{0,5})$", $filename, $regs );
$f_ext = $regs[1];

$types['image'] = array ('jpg', 'gif','png');
$types['text'] = array ('html', 'htm', 'text');
$types['music'] = array ('mp3', 'mpeg3');
foreach ($types as $k => $v) {
if (in_array($f_ext, $v)) {
return $k;
break;
}
}
echo 'type-unknown';
}

and in your readdir loop you do:
get_file_type($file)



and this is the read directory loop you want the above code inside

while (false !== ($file = readdir($handle))) {
echo "$file\n";
}

Reply With Quote
  #4  
Old June 19th, 2003, 11:34 PM
AmericanD AmericanD is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 81 AmericanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Now the above code tells you how to read the file types and stuff from a directory.

Now your second question was how to put them in an array. Well why dont u give it a try and let us see what u get

Hint : initialize an array in the begining and in the while loop increment the counter/pointer after you add the file name inside it. Same while loop which reads the directory.

all the best

Reply With Quote
  #5  
Old June 19th, 2003, 11:44 PM
omar omar is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 18 omar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks AmericanD

Thank AmericanD,
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.

Thaks alot for your help!

Reply With Quote
  #6  
Old June 20th, 2003, 07:33 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Overview Of Problem

Here is the breakdown:

1. You need to capture files that are
a. JPG
b. GIF

2. Files may be named as
a. {propertyid}.{ext}
b. {propertyid}_{num}.{ext}



This is what your code needs to do:

I. Setup your possible extensions ( jpg, gif, etc)

II. Get PropertyId ( example: 35052014 )
SIDE NOTE: Since you are working with real estate properties I suggest your PropertyId comes from the Assessor's parcel number or Its MLS Id.

III. Scan directory for files that match criteria

(see next post for the steps)
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #7  
Old June 20th, 2003, 07:37 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Requirements

Your first requirement will be to aquire a function that will not only scan a directory for files, but also filter out all the stuff you don't need.


Such a function exists and I checked throughly that it does not need modification.

Put it in your library and include it or just paste it somewhere in your script.

PHP Code:
/**
* Recursive version of glob
*
* @return array containing all pattern-matched files.
*
* @param string $sDir      Directory to start with.
* @param string $sPattern  Pattern to glob for.
* @param int $nFlags       Flags sent to glob.
*/
function rglob($sDir$sPattern$nFlags NULL)
{
 
$sDir escapeshellcmd($sDir);

 
// Get the list of all matching files currently in the
 // directory.

 
$aFiles glob("$sDir/$sPattern"$nFlags);

 
// Then get a list of all directories in this directory, and
 // run ourselves on the resulting array.  This is the
 // recursion step, which will not execute if there are no
 // directories.

 
foreach (glob("$sDir/*"GLOB_ONLYDIR) as $sSubDir)
 {
  
$aSubFiles rglob($sSubDir$sPattern$nFlags);
  
$aFiles array_merge($aFiles$aSubFiles);
 }

 
// The array we return contains the files we found, and the
 // files all of our children found.

 
return $aFiles;


Source: http://us2.php.net/manual/en/function.glob.php
sthomas at townnews dot com
11-Mar-2003 01:41

Reply With Quote
  #8  
Old June 20th, 2003, 07:44 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Re: Requirements

Quote:
Originally posted by laidbak
Your first requirement will be to aquire a function that will not only scan a directory for files, but also filter out all the stuff you don't need.

Secondly, you will need to call this function and capture its output into an array. The spec for calling this function in terms of your needs would look something like the following:

PHP Code:
 $aryPhotos rglob("./images","{$propid_*.jpg,$propid_*.gif}",GLOB_BRACE)); 

If you do not have php >= 4.3.0 this will not work.

If $propid = "369852" then the above would expand to:
PHP Code:
 $aryPhotos rglob("./images","{369852_*.jpg,369852_*.gif}",GLOB_BRACE)); 


You should not have to do any sorting of $aryPhotos as long as your files are numbered logically (e.g. 1,2,3...)
Your array should contain the filenames in order already

Reply With Quote
  #9  
Old June 20th, 2003, 08:02 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Summary

In summary, your final code would look something like this:

PHP Code:
 $aryExt = array("jpg","gif");

$propid $_REQUEST['mlsid']; // I assume the id will be in the querystring

// get the proper match pattern according to our Extension criteria
foreach ($aryExt as $e) {
 
$aryPattern[] = $propid."_*.$e";
}

$pattern join(","$aryPattern); // comma separated list

// Get the filenames
$aryPhotos rglob("./images","\\{$pattern}",GLOB_BRACE);

//print out our array
print_r($aryPhotos); 

And don't forget that you need the "rglob" function from above.

Last edited by laidbak : June 20th, 2003 at 03:25 PM.

Reply With Quote
  #10  
Old June 20th, 2003, 01:57 PM
AmericanD AmericanD is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 81 AmericanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
laidbak : i think the glob function should be called inside the parameters of the foreach ?


and i am surprized, since when did php start using lists ? i have not be updated with php since a year now

Reply With Quote
  #11  
Old June 20th, 2003, 03:21 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Quote:
Originally posted by AmericanD
laidbak : i think the glob function should be called inside the parameters of the foreach ?

Why?
Quote:

and i am surprized, since when did php start using lists ? i have not be updated with php since a year now

Ever since PHP 3

Reply With Quote
  #12  
Old June 20th, 2003, 03:56 PM
omar omar is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 18 omar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy THanks guys, but..

HI guys,
thanks for your help but I think that i may be out of luck.

The server is running PHP 4.2.2 and
as you all know the glob function
was introduced in PHP 4 >= 4.3.0.

http://us2.php.net/manual/en/function.glob.php

Thanks for your help any way!

Reply With Quote
  #13  
Old June 20th, 2003, 04:55 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Not really a huge deal...
I was prepared for that possibility.
The main issue with your problem is the logic.
The logic is simplified to just a few lines of code if done correctly.

The main work is done in the rglob function, which can easily be rewritten to using php's directory functions.

Let me know if you are going to write your own replacement function or not. If not I'll do it for you tonight.

Reply With Quote
  #14  
Old June 20th, 2003, 06:18 PM
omar omar is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 18 omar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile I would greatly appreciate your help

I would greatly appreciate your help

Thanks!

Reply With Quote
  #15  
Old June 20th, 2003, 09:33 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputa