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:
  #1  
Old June 19th, 2003, 08:24 PM
omar omar is offline
Registered User
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: 16
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: 16
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: 16
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
Registered User
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
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: 16
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
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
Registered User
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
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
Registered User
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
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
Re: I would greatly appreciate your help

Quote:
Originally posted by omar
I would greatly appreciate your help

Thanks!

No problem... I'll go ahead and knock that function out for you.

Reply With Quote
  #16  
Old June 21st, 2003, 01:47 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
Here is the finished version. Not as cool as the other one but works equally well.

I've attached the archive to this post.
Don't forget to set the mlsid in the url to something you know will show up for testing purposes.
Attached Files
File Type: zip listfiles2.zip (58.9 KB, 1211 views)

Reply With Quote
  #17  
Old June 21st, 2003, 02:13 AM
omar omar is offline
Registered User
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
Thumbs up Thanks!

Thanks laidbak!

I'll give it a shot

Reply With Quote
  #18  
Old June 21st, 2003, 02:39 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
If you have any more problems feel free to hit me by email: wil@wilmoore.com

Reply With Quote
  #19  
Old June 21st, 2003, 01:10 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: 16
So laidback i like your idea of putting stuff in a list and doing it. didn't even think of it.

What does the _* do in the pattern match ? especially the _

Reply With Quote
  #20  
Old June 21st, 2003, 01:51 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
AmericanD,

The _* is just a simple wildcard expression.
The "_" part is not part of the wildcard.
Essentially what we are doing is looking for a file with the first part of the name matching the mlsid.

So if the mlsid is "3412498", then we would be looking for a file that looks like the following:

3412498_1.jpg
3412498_1.jpg
3412498_1.jpg

Instead of the * we really could have used ?, however I went for the * because there could be a situation where he may need to name the files such that there is a padded zero... for instance:

3412498_01.jpg
3412498_02.jpg

This way the patter would still match.

The only drawback to this patter is that it is a little open. If there is a file named:

3412498_xyz.jpg
it will be a match... But this is totally up to him... he can modify that one pattern and it will all be taken care of.

Reply With Quote
  #21  
Old June 21st, 2003, 05:01 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: 16
Thanks for clarifying I appreciate it.. It reminded me of our regular expression class we took in our first year computer science undergrad.

Reply With Quote
  #22  
Old June 21st, 2003, 11:12 PM
omar omar is offline
Registered User
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
Thumbs up Thanks Wil

Thanks Again Wil

Reply With Quote
  #23  
Old June 24th, 2003, 07:58 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
So, Omar, did everything work out?

Reply With Quote
  #24  
Old June 25th, 2003, 12:40 AM
omar omar is offline
Registered User
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 Yes Wil

Yes Wil,
the website is not finished at the moment
but every thing seems to be on the right
page so so. I was able to use your script
just as it is. Once again I would like to say
a big thank you!!

btw.
do you have any good links to website where I can learn how to use Regular Expressions.


thanks again!

Omar Dixon



Last edited by omar : June 25th, 2003 at 12:42 AM.

Reply With Quote
  #25  
Old June 25th, 2003, 02:28 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 785 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 17
Hey, no problem at all Omar... glad to help.

Here are some pretty good resources on Regular Expressions:

Good set of Resources
http://aspsmith.com/re/
http://www.robotwisdom.com/net/regexres.html

Wizard to help debug Regular Expressions
http://www.funduc.com/search_replace_wizard.htm

And I would suggest getting the following book as a reference:
Mastering Regular Expressions (O'Reilly) - Jeffery E. F. Friedl

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > php - Find files (photos*.jpg) - return results to an array


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap