0

I have a situation where the client will be uploading a ton of pdf files, and then on the front-end we need a tool to search for key-terms within these files. Sometimes that key-term is in the file name (could possibly break apart with php to get the terms?) and sometimes the key-term is just in the file itself.

php to break apart the terms:

                <?php 
                    // break apart file name, grab first word
                    $str = '{title}'; 
                    $arr1 = preg_split("/[_,\- ]+/", $str); 
                ?>
                <?php echo $arr1[0]."\n"; ?>

Which takes a file name like: FFOBBULUSE000002_Harlequin GDP 3.5g TruFlower Cup.pdf and outputs the first term like: FFOBBULUSE000002

I've looked into Channel Files, and of course native EE search and File Entries, but none are really giving me what I need. Native search doesn't seem to be able to search native files, nor Channel Files?

Plus, there are thousands of these files, so I don't really want to load them all into a template and then filter. Hoping for a less resource-heavy way of doing this.

Example:

Uploaded file: FFOBULN2F000004_9lb 600mg Vape Cartridge.pdf

Someone may search for the term: FFOBULN2F000004 - that term needs to be able to pull up this file in the results, and also shouldn't be case sensitive.

Any ideas?

UPDATE:

I'm getting really close via using Stash. Here's what I have so far:

{exp:stash:set_list parse_tags="yes" name="my_entries"}

    {exp:file:entries directory_id="19" orderby="date" sort="desc" limit="3000" disable="relationships|categories|category_fields|member_data|pagination"}

        {!--<!-- [STORE PHP VALUE IN EE VARIABLE TO USE LATER] -->--}
        <?php 
            // break apart file name, grab first word
            $str = '{title}'; 
            $arr1 = preg_split("/[_,\- ]+/", $str);
            //echo $arr1[0]."\n"; 
        ?>
        {stash:batch_value}<?php echo $arr1[0]."\n"; ?>{/stash:batch_value}

        {stash:batch_number}{exp:stringer:striptags words="1"}{title}{/exp:stringer:striptags}{/stash:batch_number}



        {stash:entry_title}{title}{/stash:entry_title}
        {stash:count}{count}{/stash:count}
        {stash:url}{file_url}{/stash:url}

    {/exp:file:entries}

{/exp:stash:set_list}

{exp:stash:get_list name="my_entries" match="#(?i){freebie_3}#" against="batch_number"}

    <div class="item {batch_number} field-id-{count}">
        <p><span>{entry_title}</span> // {batch_value} - <a href="{url}" title="Download {entry_title}" download>Download</a></p>
    </div>

        {if no_results}
            {exp:stash:set name="my_message"}No results{/exp:stash:set}
        {/if}

{/exp:stash:get_list}        

This is working almost perfectly.

But now, when I have php parse set to input, I get the full file name (doesn't break it apart and give me the first word). When I have php parse set to output, I get this error:

NOTICE UNDEFINED VARIABLE: ARR1 ee/legacy/libraries/Functions.php(682) : eval()'d code, line 46
Severity: E_NOTICE

I've tried using stringer to get the first word, but it doesn't break on underscores, and I need that :/

1

1 Answer 1

1

I got some feedback from the developer of Stash (Thanks again, Mark!) on this via Twitter, and he advised not to do it this way.

enter image description here

So I went back to the drawing board. I ended up having our programmer help me out and write some custom php to perform a search in the file directory, and return the results in my template, rather than use any EE tags.

Basically, the php converts all of the file names to lower-case. Then searches the filenames to match against the key-term entered. Removes the server path from the file so we have a clean URL and display file name. Then writes the results to the template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.