0

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.

hderic
  • 3
  • 3
  • Does this answer your question? [Search by phrase in files(doc,docx,xlsx,pdf) in php](https://stackoverflow.com/questions/37537900/search-by-phrase-in-filesdoc-docx-xlsx-pdf-in-php) – Ken Lee Feb 22 '23 at 17:55
  • Thanks for responding, I'll have a look at it asap! – hderic Feb 22 '23 at 19:53
  • So, it's definitely useful. But I'm not using SQL(and don't really know anything about it yet(noob)) I just have a bunch of files in a directory tree. So those parts are throwing me off. The code in that post about converting file types to text is going to be very useful though! It seems like what I'm trying to do would happen all the time, at least common enough to be in some library, but I've been searching all day since posting and don't see any. – hderic Feb 22 '23 at 20:57
  • Anything that is not plain text (like Json,XML,csv), must be parsed in a special way (like PDF, XLSX). So if you for example parse a pdf with php to find an match, the hole process becomes very inefficient. Because you cannot simple get the content an check for a needle in the string. Maybe that is too much magic for one task. And also: What if someone just search 'a'? So 'user input based live search recursively in x files' is never a good approach. – Foobar Feb 23 '23 at 11:57

0 Answers0