You are here: Re: Does anyone use Glob? « All PHP « IT news, forums, messages
Re: Does anyone use Glob?

Posted by Steve on 11/16/07 16:56

"Tom Mackay" <nospam@nospam.com> wrote in message
news:13jri2nqr1phc22@corp.supernews.com...
>
> "Steve" <no.one@example.com> wrote in message
> news:Spj%i.25$ug7.5@newsfe02.lga...
>>
>> "Tom Mackay" <nospam@nospam.com> wrote in message
>> news:13jrfeiaa8dscca@corp.supernews.com...
>>> "Steve" <no.one@example.com> wrote in message
>>> news:uqY_i.7$dY3.2@newsfe02.lga...
>>>>
>>>> "Mike" <me@privacy.net> wrote in message
>>>> news:13jo2saj2oacnda@corp.supernews.com...
>>>>> Hi
>>>>>
>>>>> Does anyone use glob to search?. I am wondering how good/effective it
>>>>> is
>>>>>
>>>>> http://de3.php.net/manual/en/function.glob.php
>>>>
>>>> i've been using it for years in the form of:
>>>>
>>>>
>>>> function listFiles($path = '.', $extension = array(), $combine = false)
>>>> {
>>>> $wd = getcwd();
>>>> $path .= substr($path, -1) != '/' ? '/' : '';
>>>> if (!chdir($path)){ return array(); }
>>>> if (!$extension){ $extension = array('*'); }
>>>> if (!is_array($extension)){ $extension = array($extension); }
>>>> $extensions = '*.{' . implode(',', $extension) . '}';
>>>> $files = glob($extensions, GLOB_BRACE);
>>>> chdir($wd);
>>>> if (!$files){ return array(); }
>>>> $list = array();
>>>> $path = $combine ? $path : '';
>>>> foreach ($files as $file)
>>>> {
>>>> $list[] = $path . $file;
>>>> }
>>>> return $list;
>>>> }
>>>>
>>>
>>> Hi
>>> I just tried your script but nothing happens. What is it supposed to
>>> do?
>>
>> list files in a directory...as in:
>>
>> ==============
>>
>> <?
>> function listFiles($path = '.', $extension = array(), $combine = false)
>> {
>> $wd = getcwd();
>> $path .= substr($path, -1) != '/' ? '/' : '';
>> if (!chdir($path)){ return array(); }
>> if (!$extension){ $extension = array('*'); }
>> if (!is_array($extension)){ $extension = array($extension); }
>> $extensions = '*.{' . implode(',', $extension) . '}';
>> $files = glob($extensions, GLOB_BRACE);
>> chdir($wd);
>> if (!$files){ return array(); }
>> $list = array();
>> $path = $combine ? $path : '';
>> foreach ($files as $file)
>> {
>> $list[] = $path . $file;
>> }
>> return $list;
>> }
>> $files = listFiles('c:/inetpub/wwwroot/images', 'jpg', true);
>> $images = array();
>> foreach ($files as $file)
>> {
>> $fileInfo = pathinfo($file);
>> $handle = fopen($file, 'r');
>> $fileInfo = array_merge($fileInfo, fstat($handle));
>> fclose($handle);
>> for ($i = 0; $i < 13; $i++){ unset($fileInfo[$i]); }
>> echo '<pre>' . print_r($fileInfo, true) . '</pre>';
>> }
>> ?>
>>
>> ==============
>>
>> if you want to search for multiple extensions, then the listFiles arg
>> would be an array...as in:
>>
>> array('jpg', 'gif', 'tif')
>>
>> make sense?
>
>
> OK thanks.
> FYI I have a similar list script i have been using for a while but
> would like to replace this will a search instead of a full listing.
> i.e. - search for file xxxx rather than going through the list to see if
> it is present.
>
> <?php
> $path = "/usr/local/apache/share/default/";
> $dir_handle = @opendir($path) or die("Unable to open $path");
> while ($file = readdir($dir_handle)) {
> if($file == "." || $file == ".." || $file == "index.php" )
> continue;
> echo "<a href=\"$file\">$file</a><br />";
> }
> // Close
> closedir($dir_handle);
> ?>
>
> Any suggestions how to do this or point me in the right direction?.

from you example above:

$wd = getcwd();
chdir($path);
$files = glob('index.php');
chdir($wd);

that's it.

if you were so inclined, just modify my listFiles function at this line:

$extensions = '*.{' . implode(',', $extension) . '}';

to:

$extensions = '{' . implode(',', $extension) . '}';

i'd also change the variable names to $find (was $extension) and $search
(was $extensions). the only other change you'd need to note is when calling
the function. if searching by extension, you'd have to include the asterisk
yourself...

array('*.jpg', '*.png', 'index.php')

for example. that should list all jpg and png images and any file named
index.php. see if that works for you.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация