Forum Moderators: coopster

Message Too Old, No Replies

indexing

help needed

         

zinc

3:52 pm on May 19, 2004 (gmt 0)

10+ Year Member



does anyone have or know of a script that will index all the images in a directory. ither onto a php,html page or will even add it info to a mysql database.

thanks in advance

httpwebwitch

5:04 pm on May 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not the prettiest code, and it certainly could be improved, but it works. try it.


$rootdirectory = "/";
$imagelist = array();
$ignoredirarray = array('secret','hidden','stats','hideme');
$ignorefilearray = array('secretimg.gif');
$URLpath=Array();
browse_images($rootdirectory,$URLpath);

print ("There are ".count($imagelist)." images.");

for ($i=0;$i<count($imagelist);$i++){
print("<BR>".$imagelist[$i]);
}

function browse_images($dir_name, $URLpath){
global $imagelist;
global $ignoredirarray;
global $ignorefilearray;
print("<table border='0' cellpadding=0 cellspacing=0 width='100%'>\n");
$dir = opendir($dir_name);
while($file = readdir($dir)){
if($file!= "." && $file!= ".."){
if(is_dir($dir_name . $file)){
$found_dirs[] = $file;
}else{
$found_files[] = $file;
}
}
}
closedir($dir);

if($found_dirs){
sort($found_dirs);
for($i = 0; $i < count($found_dirs); $i++){
if($found_dirs[$i]!= "." && $found_dirs[$i]!= ".."){
if(! in_array( strtolower($found_dirs[$i]),$ignoredirarray)){

Array_push($URLpath,$found_dirs[$i]);
browse_images($dir_name . $found_dirs[$i] . "/",$URLpath);
Array_pop($URLpath);

}
}
}
}

if($found_files){
sort($found_files);
for($i = 0; $i < count($found_files); $i++){
if(! in_array( $found_files[$i],$ignorefilearray)){
$extension = preg_match('/\.(.*)$/',$found_files[$i],$out);
$extension = $out[1];
$fullpath = implode("/",$URLpath)."/".$found_files[$i];
if ($extension=='gif' ¦¦ $extension=='jpg' ¦¦ $extension=='png' ¦¦ $extension=='jpeg'){
Array_push($imagelist,$fullpath);
}
}
}
}
}

WhosAWhata

10:33 pm on May 19, 2004 (gmt 0)

10+ Year Member



<?
$images = glob("dir/{*.jpg,*.gif,*.png}",GLOB_BRACE);

?>

now you have an index of ever .jpg, .gif, and .png in dir/

access them like this $images[0] or $images[1]

WhosAWhata

10:39 pm on May 19, 2004 (gmt 0)

10+ Year Member



if you want to display them on a page

<?
$numcols = "4"; //number of pics in a column
$images = glob("dir/{*.jpg,*.gif,*.png}",GLOB_BRACE);
$count = count($images);
if(!$count) die;
$html = "<table cols=\"".$numcols."\">";
foreach($images as $key => $name){
$r = $key % $numcols;
if (!$r) $html .= "<tr>";
$html .= "<td><img width=300 src=\"".$name."\"></td>";
if($r == $numcols-1) $html .= "</tr>";
}
echo $html."</table>";
?>

haven't tested, but should work

jatar_k

10:41 pm on May 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



more than one way to skin a cat

<? 
$mydir = "/path/to/dir/";
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
// this will echo the name of each file
// it can be changed to do whatever you like
echo "<p>$entry\n";
}
}
$d->close();
?>

WhosAWhata

10:44 pm on May 19, 2004 (gmt 0)

10+ Year Member



but that returns every file
glob() just returns specified extentions
i suppose it depends on your needs
i think glob is just a lot easier
...and more fun to say

jatar_k

10:56 pm on May 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yeah sry, I could have gone farther on that, kinda forgot, got sidetracked

glob is the most fun to say and the easiest though

httpwebwitch

5:02 pm on May 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wow! glob() does in one line what my script hacks up with 100. Thanks for exposing me to this function which I had never heard of before!

(I wonder what other valuable nuggets are out there, but never mentioned in my printed manuals, Wrox, O'Reilly, etc.? That's a subject for another thread)

WhosAWhata

10:54 pm on May 20, 2004 (gmt 0)

10+ Year Member



i actually posted a thread to find all of the .whatever files in a directory about a year ago and that is when i was first exposed to glob()...

zinc

1:13 am on May 21, 2004 (gmt 0)

10+ Year Member



thanks for all the help guys , the scipt above looks cool but i really dont know how i should be using it. andyone with msn messenger would be great

[edited by: jatar_k at 6:50 am (utc) on May 21, 2004]
[edit reason] no specifics thanks [/edit]

zinc

1:21 am on May 21, 2004 (gmt 0)

10+ Year Member



i need it to list the url and name and type of file.

i want to be able to list the files at this url

basically all i want to do is make a search box that will find files in that directory matching whatever is put into the search. there are not html files at all in the dir above, its only image files and sub directories

thanks again

[edited by: jatar_k at 6:49 am (utc) on May 21, 2004]
[edit reason] no specifics thanks [/edit]

WhosAWhata

5:18 pm on May 21, 2004 (gmt 0)

10+ Year Member



send me a private message with your MSN name and exactly what you need

I think i might be able to lend a hand
(you can't post everything in the actual forum, but anything goes in the StickyMail)

coopster

8:41 pm on May 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glob is cool, but it's slower. Here's a link with some more information regarding the glob function...

[webmasterworld.com...]

zinc

2:22 pm on May 25, 2004 (gmt 0)

10+ Year Member



thanks for the replys guys, but as i said when i look at a bit of script, its just text to me, i have no idea in what way i should be using this, should i be making a new php page and copying the text into it, and what are the bit i need to change to get it to work on my dir.