I have created a company intranet page on a synology NAS that contains links to a bunch of internal company documents (How Tos, processes, etc). It has multiple pages that use a short PHP script to create lists of links to files in folders on the server organized by department.
<?php
$dir = "./files_to_serve/Fabrication/How_To"; //Directory to be iterated for links
if (is_dir($dir)){//make sure its a directory
if ($dh = opendir($dir)){// Open a directory, read its contents
while (($file = readdir($dh)) !== false){
if ($file != "." and $file != "..") { // skip links "." and ".."
$encoded_file = rawurlencode($file);// replace spaces in filenames with %20, etc
echo "<a href=$dir/$encoded_file target=\"_blank\">$file</a><br><br>"; //create download links to files
}
}
closedir($dh);
}
}
?> <!-- End PHP -->
I am attempting to create a search feature that would search all the folders. So, where that script lists all files in "./files_to_serve/Fabrication/How_To", this would search the files in "./files_to_serve" for a string input via a html form and return a html page listing links to the files with matches, presumably sorted for relevance.
I set out to create this script in PHP, but as I go I think of more and more issues, like the files in ./files_to_serve could be any type. For example, .xlsx. And my initial plan of using grep wont work on .xlsx.
I also feel like I might be reinventing the wheel. Is there some function or technique that will already do what I am trying to do? My google-fu hasn't reveled anything, but I am very new to all of this and am likely asking the question poorly.
Basically I want to create a list of links to all files recursively in a directory that contain a match to a string.
Can anyone recommended an existing way to do this? Or suggest a good way to search the contents of a variety of file types so I can attempt to implement it myself?
Thanks! and sorry if it's obvious, this is the first website of any real complexity I have attempted.
Edit - As stated in comment my question is different from suggested answer because I am attempting to search files in a directory(recursively) not a SQL database.