We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    I would like to display text on a page that changes, dependent on whether or not a file exists. Like the phx:fileexists function in Evo
    http://wiki.modxcms.com/index.php/PHx/CustomModifiers#phx:fileexists

    I have tried to do this using a snippet based on phx:fileexists in conjunction with output modifiers

    I have a snippet called: fileexists

    <?php
    $condition[] = intval(file_exists($output));
    ?>


    My page has the following code:
    [[fileexists:is=`assets/path/to/file.pdf`:then=`Yes`:else=`No`]]


    This returns No, although the file does exist.

    I have tried various permutations of the above to no avail. I think I am a bit out of my depth with this one!

    If anyone can point me in the right direction it would be greatly appreciated.

    This question has been answered by BobRay. See the first response.

      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
    • discuss.answer
      • 3749
      • 24,544 Posts
      Output modifiers are meant to modify the results of a MODX tag that returns the content of a resource field, chunk, or TV; or the return value of a snippet. That' not really what you want, which is why it's so tricky.

      It could be done with the If snippet, but for something that simple, I usually just use a very short custom snippet and put the desired output in the snippet. It's faster and easier than using output modifiers or If:


      [[!FileExists? &file=`path/to/file`]]



      /* FileExists snippet */
      $path = $modx->getOption('file', $scriptProperties, '');
      return file_exists($path)? 'Your Text Here' : '';


      BTW, you can also do this in your tag:

      [[!FileExists? &file=`[[++assets_path]]path/to/file`]]


      That way it will still work if you move the site. [ed. note: BobRay last edited this post 11 years, 7 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 13428 ☆ A M B ☆
        • 1,031 Posts
        An output filter has to work on an existing MODX tag as Bob already said.

        One option to use your code as output filter would be using it on a placeholder that exists everywhere.
        [[*id:fileexists=`…`:then=`…`]]

        should work. Just as an option, it is not very efficient.
          • 38783
          • 571 Posts
          Thank you both for getting back to me. You help is greatly appreciated.

          Jako, I have gone with Bob's solution, purely because I had the chance to try it out before you posted yours. But thank you anyway.

          Bob, I just had to make one change to your code, which was to swap the $ in $file= to an ampersand. ie: &file=.

          [[!FileExists? &file=`path/to/file`]]


          /* FileExists snippet */
          $path = $modx->getOption('file', $scriptProperties, '');
          return file_exists($path)? 'Your Text Here' : '';


          This is really useful. I am building a site for a club whose members have profiles available as PDFs. I wanted to be able to ensure that if a members page was visited then their PDF would be available to download if it was present, but if their PDF was not present a message would explain they have no profile PDF at present. This relies on the PDF being named according to values stored in that users extended profile. So the following code was what I finally used:

          [[!FileExists? &file=`assets/documents/member-details/[[+memberId]]_[[+firstName]]-[[+lastName]]_Profile.pdf`]]


          <?php
          /* FileExists snippet */
          $path = $modx->getOption('file', $scriptProperties, '');
          return file_exists($path)? '<p>View or download [[+firstName]]\'s profile as a <a href="assets/documents/member-details/[[+memberId]]_[[+firstName]]-[[+lastName]]_Profile.pdf">PDF</a></p>' : '<p>[[+firstName]] [[+lastName]] does not have a profile available at present.</p>';
            If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

            email: [email protected] | website: https://andytough.com
            • 3749
            • 24,544 Posts
            I make that mistake when I've been writing a lot of PHP code. wink

            I'm glad it works for you.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting