Get file folders structure for specific files

Silverstripe Version: 4*

Question:

I want to display the folder structure for the files that are found through frontend search.
So for example:
In the admin/assets/ section I created a subfolder within Uploads folder and then added another subfolder and then uploaded a file:
Uploads/Subfolder_1/Subfolder_2/MyFile.pdf
I extended the Search form for customizing the results on the frontend.
Now I need to display the folder structure for the files that are found through search
For the example that would be: Uploads / Subfolder_1 / Subfolder_2

class MySearchForm extends SearchForm
{
public function getResults()
    {
    $request = $this->getRequestHandler()->getRequest();
    $keywords = $this->getKeywords($request);
    $resultsPerPage = $this->getPageLength();
    $start = max(0, (int)$request->requestVar('start'));

    $booleanSearch =
            strpos($keywords, '"') !== false ||
            strpos($keywords, '+') !== false ||
            strpos($keywords, '-') !== false ||
            strpos($keywords, '*') !== false;
    $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $resultsPerPage, "\"Relevance\" DESC", "", $booleanSearch);
    $results = $this->canViewPost($results);
    $results = $this->isFileInResult($results);
    return $this->processResult($results, $keywords);
}

private function processResult($results, $keywords)
{
   $processedKeywords = str_replace(array('"', '*'), '', $keywords);
   foreach ($results as $result) {
            $result->Content = preg_replace('/(' . preg_quote($processedKeywords, "/") . ')/i', "<span class=\"highlight-search-result\">\$0</span>", $result->Content);
            $result->Title = preg_replace('/(' . preg_quote($processedKeywords, "/") . ')/i', "<span class=\"highlight-search-result\">\$0</span>", $result->Title);
            
          // Is there a simple way to get the folder structure?
           if ($result->ClassName === "SilverStripe\Assets\File") {
                $result->FileFolders = ???
            }
}
   return $results;
}

Is there a simple method to get the folder structure? Or the full path?

I got the folders from FileFilename

$result->folders = str_replace('/', ' ยป ' ,pathinfo(File::get_by_id($result->ID)->FileFilename, PATHINFO_DIRNAME));

Seems to work :slight_smile:

Hi @Bagus

We can get assets folder structure path by using ASSETS_PATH.

You can use it by writing this code: ASSETS_PATH/Subfolder_1/Subfolder_2/MyFile.pdf

ASSETS_PATH is defined as constants.

Hi,
how can I use this? I have File ID at my disposal.

As per your code you can get it by using below line.

ASSETS_PATH/File::get_by_id($result->ID)->FileFilename