|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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!. |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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"; } |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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! |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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:
Source: http://us2.php.net/manual/en/function.glob.php sthomas at townnews dot com 11-Mar-2003 01:41 |
|
#8
|
|||
|
|||
|
Re: Requirements
Quote:
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:
If you do not have php >= 4.3.0 this will not work. If $propid = "369852" then the above would expand to: PHP Code:
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 |
|
#9
|
|||
|
|||
|
Summary
In summary, your final code would look something like this:
PHP Code:
And don't forget that you need the "rglob" function from above. Last edited by laidbak : June 20th, 2003 at 03:25 PM. |
|
#10
|
|||
|
|||
|
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 |
|
#11
|
||||
|
||||
|
Quote:
Why? Quote:
Ever since PHP 3 |
|
#12
|
|||
|
|||
|
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! |
|
#13
|
|||
|
|||
|
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. |
|
#14
|
|||
|
|||
|
I would greatly appreciate your help
Thanks! |
|
#15
|
|||
|