I have a website that lists pdf files for downloading, with links to each file.

What I want to do now is design a search page that will look in the folder that contains these docs and search for docs that match a partial name.

I have created search pages for MySQL before - do I have to create a table with file names and links and then do a search of MySQL?

TIA,

jej1216

Member Avatar for Rhyan

It is possible, however it is said that such functions are subject to vulnerability.

Still - I have made such thing and it is quite self-maintaining page you will get out of it.
See the PHP manual for functions readdir and scandir. It is quite simple to do it.

Member Avatar for iamthwee

If you know what OS they use. Searching for folders/files is doable.

I have a website that lists pdf files for downloading, with links to each file.

What I want to do now is design a search page that will look in the folder that contains these docs and search for docs that match a partial name.

I have created search pages for MySQL before - do I have to create a table with file names and links and then do a search of MySQL?

TIA,

jej1216

It is possible to search the directories for files using the PHP file functions like mentioned. See: http://www.php.net/manual/en/function.opendir.php

You'd get better performance by indexing the files in a db like you said, since searching database rows is much faster than scanning a directory, especially if you have many files.

commented: Good point about the indexing. +11

Thanks for all of the quick replies!

I'll try the opendir and scandir and see if that is quick enough for our purposes. If it's too slow, I'll do the database approach.

Rhyan - you said "It is possible, however it is said that such functions are subject to vulnerability." What vulnerablility? Hacking?

Thanks everyone.

jej1216

they can be exploited to form a DoS attack
servers which have PHP safe mode enabled (most hosted ones) dont allow these potentially dangerous commands

hi friends,
i have query...
how can i find specific file from different directory, in my PC, by PHP code ??
Is there any in-biult command??

Thanx,

The php/html below should provide you with what you are looking for. The php script, in conjucntion with the provided html search form allows you to scan a folder and returns a link in your browser. You may then download the file using the link provided.

What I would like to see is a search form which would allow you to search for multiple items with different names within a folder, the result being that links to those items would be returned in your browser. If anyone has any ideas, please reply.

Your php code:

<?php
$dir = 'name_of_your_directory'; 
$exclude = array('.','..','.htaccess'); 
$q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; 
$res = opendir($dir); 
while(false!== ($file = readdir($res))) { 
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { 
echo "<a href='$dir/$file'>$file</a>"; 
echo "<br>"; 
} 
} 
closedir($res); 
?>

Your html search form code:

<form action="name of php file.php" method="get"><input name="q"
type="text"> <input type="submit"></form>

Golden Bear. You have mentioned a good script. But what if we limit the search to a specific word or string. For example I want to that if 1234 is entered then the 1234 file is shown. using only 12 must not show the result.

How to output, if the relevant file is not in the foler it shold ive the message file not found.

Please guide

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.