PDA

View Full Version : Script Needed For Image Find


paddy15
06-28-2004, 01:17 AM
I was wondering if anyone knew of a free script that searches image files, and displays the results as links to the image. There are alot of search scripts out there, but they only search for .txt and .html files using keywords. I am don't know a massive amount about cgi scripts but am I right to believe I need some sort of "file::find"

dswimboy
06-30-2004, 09:47 PM
how do you want to search for files? by date, size, or something else. searching for 'content' would be hard, unless you had a database, one field with the picture description, another with the filename

dswimboy
06-30-2004, 09:52 PM
crap...double post...sorry

paddy15
07-01-2004, 02:27 AM
i want to search by file-name alone. Is that possible?

MattJakel
07-01-2004, 06:17 AM
It's definitely possible to search by file name.

A couple of questions though:

The images you are searching are all on your server, right?
Are they all in a single directory or spread throughout the site?
Do you want to search for the exact file name or do you want something like snaek.jpg to come up with snake.jpg?

paddy15
07-01-2004, 09:45 PM
Yep, they are on my server

They are all in 4 sub-directories within 1 directory
../graphics/1
../graphics/2
../graphics/3
../graphics/4

I would like to search specific word search and boolean
e.g. If you search for 'Snake', images such as 'snake.jpg' , 'red-snake.jpg' and 'snakeeggs.jpg' would appear.

dswimboy
07-01-2004, 09:59 PM
the File::Find module, is correct. here's a quicky


use File::Find;

@DIRLIST = ( '../graphics/1', '../graphics/2', '../graphics/3', '../graphics/4');

sub search {
if ($_ =~ /snake/) {
print "$File::Find::name\n";
}
}

find (\&search, @DIRLIST);

paddy15
07-02-2004, 12:35 AM
im not good with cgi-scripts, but I see the word 'Snake' in there. I dont want to search for a single word, I just wana script where a visitor types in a query and comes back with results (with a link to the particular image).
I am sorry if I did not make that clear.

dswimboy
07-02-2004, 09:41 PM
use File::Find;
use CGI;

$q = new CGI;

$query = $q->param("search");

@DIRLIST = ( '../graphics/1', '../graphics/2', '../graphics/3', '../graphics/4');

print "Content-type: text/html\n\n";

sub search {
if ($_ =~ /$query/) {
print "$File::Find::name\n";
}
}

find (\&search, @DIRLIST);

this will take the field named "search" in your form, and look through the filenames, printing out results

paddy15
07-03-2004, 02:03 PM
It all works great apart from a url-link to the image. Can that be achieved?

dswimboy
07-03-2004, 10:57 PM
of course...i kinda hoped you would be able to figure out you needed to add an HTML link to the output,but here you go:

sub search {
if ($_ =~ /$query/) {
print "<a href='$File::Find::name'>$_</a>\n";
}
}


if you look at the file::find documentation on cpan (http://search.cpan.org/~nwclark/perl-5.8.4/lib/File/Find.pm) it may help.

paddy15
07-04-2004, 01:54 AM
Oh dear! There is a prob. For the gallery dirs, i said that they where just ../graphics/1 ect. But all files go into a 'htdocs' file. So, the images urls are wrong.
Example, It displays the url of cat as http://www.jam.ukcool.com/htdocs/graphics/1/cat.bmp
However, its wrong as the url should not contain 'htdocs', and only the server path should contain it. The root of the server path is
/freeola/users/4/7/sr0260774
But all public files go into /freeola/users/4/7/sr0260774/htdocs. This means that I have to put ../htdocs/graphics for the search to find the images, but by doing this gives the wrong url.
Sorry for any inconvience

dswimboy
07-04-2004, 08:01 PM
this is a band-aid, not a real fix. it'll work though:

sub search {
if ($_ =~ /$query/) {
$path = $File::Find::name;
$path =~ s/htdocs\///i;
print "<a href='$path'>$_</a>\n";
}
}

paddy15
07-04-2004, 11:22 PM
Thank you ever so much.
Sorry if I am pestering but this will be my last query.
*Is there a way so the search is case insensitive
*Is there a way of the script printing "No results found" if no images are found?

Your replies are much appriciated, and If you have a website of your own I will gladly make a link to it on my website.

dswimboy
07-05-2004, 07:27 PM
use File::Find;
use CGI;

$q = new CGI;

$query = $q->param("search");

@DIRLIST = ( '../graphics/1', '../graphics/2', '../graphics/3', '../graphics/4');

print "Content-type: text/html\n\n";

sub search {
if ($_ =~ /$query/i) {
$results++;
$path = $File::Find::name;
$path =~ s/htdocs\///i;
print "<a href='$path'>$_</a>\n";
}
}

find (\&search, @DIRLIST);

unless ($results) {
print "No Results found\n";
}


i highlighted the changes. i'm glad i could help you, that's why i come to this forum. sad to say, i do not have a site of my own (i should). that is the first offer i've gotten for a link, and is very kind of you!

paddy15
07-06-2004, 01:00 AM
It's great to hear!
I was wondering then if there is a way a script could be made to show a list of the images (with url links) in alphabetical order. (Basically, an index of all the files). Can it also be done with a list of uploads in the last 24 hours onn top of the index?
I wouldn't like you to stain yourself if its too complicated though

These would be also very very helpful for my website.
Thank you in advance

dswimboy
07-06-2004, 03:38 AM
of course it is possible. the method is slightly different. i'll search through the files, and store their name and path in a hash, which is a type of variable. then when the search is done, we'll sort the hash by name, then display the results.
use File::Find;
use CGI;

$q = new CGI;

$query = $q->param("search");

@DIRLIST = ( '../graphics/1', '../graphics/2', '../graphics/3', '../graphics/4');

print "Content-type: text/html\n\n";

sub search {
if ($_ =~ /$query/i) {
$path = $File::Find::name;
$path =~ s/htdocs\///i;
$results{$_} = $path;
}
}

find (\&search, @DIRLIST);

if ($results) {
@results = sort {$a cmp $b} (keys %results);
foreach $result (@results) {
print "<a href='$results{$result}'>$result</a>\n";
}
} else {
print "No Results Found\n";
}

i'm not positive this will work. my hash sorting hasn't really been applied yet. check it out, and let me know if it breaks.

paddy15
07-06-2004, 04:19 AM
Is that a search? I was just wanting an file lister
And is the 'uploads in last 24 hours' possible?

dswimboy
07-06-2004, 04:27 AM
that is a searcher...lister...

opendir(DIR, $dirname) or die "can't open $dirname: $!";
while (defined ($file = readdir(DIR))) {
# do something with $dirname/$file
}
closedir(DIR)

i'm not gonna write out all the code for you this time.. if you're going to be using perl a lot you should either learn it yourself, or hire someone to do it for you. O'Reilly's "Learning Perl" and "Perl Cookbook" taught me nearly everything i know about perl. although they are rather expensive, i think it's worth it. you can prolly find some good tutorials thoruhg a quick google as well.

paddy15
07-06-2004, 04:32 AM
Thanks for your generous help.
I think I will look into perl myself, even though it seems very complicated.
But hay, I mastered HTML
Thanks again, you really aided in making my website better!

Paddy :)