We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49659
    • 86 Posts
    I'm creating a form to upload files, and am using the FileUpload addon.

    In the fuOuterTpl chunk, there is this HTML:
    <p>Note: Only files with the following extensions will be accepted ([[+fileupload.extensions]])</p>

    The [[+fileupload.extensions]] placeholder is a comma-separated string of values. I want to parse that out and ultimately display the values in a HMTL list.

    Any help with this is greatly appreciated!
      • 52618
      • 10 Posts
      Hi Kjf,

      See if below helps out, works using a snippet with a tpl and a csv of file extensions.

      Snippet:
      <?php
      /***
       * USAGE
       * [[!FileExtensionParser?
       *  &extensions=`[[+fileupload.extensions]]`
       *  &tpl=`ExtensionListTpl`
       * ]]
      ***/
      
      $tpl        = $modx->getOption('tpl',$scriptProperties,'ExtensionListTpl');
      $extensions = $modx->getOption('extensions',$scriptProperties,'.doc,.odt,.pdf,.rtf,.tex,.txt,.wks,.wpd');
      
      $extensions = explode(',',$extensions);
      
      foreach ($extensions as $x) {
          $output .= $modx->getChunk($tpl,array(
              'extension'=> $x
          ));
      }
      return $output;
      


      Chunk
      <li>[[+extension]]</li>
      
        • 49659
        • 86 Posts
        Very helpful, thank you.

        I'm encountering one problem though. This line:
        $extensionsArr = explode(',',$extensions);

        ..creates an array, but the item count is one. All the comma separated values exist in the first array value only. The resulting HTML is:
        <li>avi,flv,wmv,mov,mp4,mpg,mpeg,bmp,gif,jpg,jpeg,pdf,png,tiff</li>


        I've tested the resulting array using
        is_array($extensionsArr)
        and it passes true. But the array
        count($extensionsArr)
        is 1. Only $extensionsArr[0] contains a value, and that value is the comma-separated string.

        I've tried creating the array using this:
        $extensionsArr = str_getcsv($extensions, ',');

        ..but the result is the same - an array with a single value.

        Any ideas on what's going on here? Thanks so much for your help.
          • 3749
          • 24,544 Posts
          You want to duplicate the code FileUpload uses to get the extensions:

          $extensions = $modx->getOption('extensions', $scriptProperties, '');
          if (empty($extensions)) {
              $extensions = $modx->getOption('upload_files', null, '');
          }


          I don't see how what you're describing is possible, though.

          Give this a try to get a look at the results:

          $extensions = $modx->getOption('extensions', $scriptProperties, '');
          if (empty($extensions)) {
              $extensions = $modx->getOption('upload_files', null, '');
          }
          
          $extensionsArr = explode(',',$extensions);
          return '<pre>' . print_r($extensionArr, true) . '</pre>';
          

            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
            • 49659
            • 86 Posts
            Hi Bob,

            I tried retrieving the comma-separated string, per this code:
            $extensions = $modx->getOption('extensions', $scriptProperties, '');
            if (empty($extensions)) {
                $extensions = $modx->getOption('upload_files', null, '');
            }

            ..exploding it into an array per this code:
            $extensionsArr = explode(',', $extensions);

            ..and test printing the array as you suggested:
            $output = '<pre>' . print_r($extensionArr, true) . '</pre>';
            return $output;

            The print_r output is null or blank. the resulting HTML source reads like this:
            <pre></pre>

            I can print this however:
            $output = $modx->getChunk($tpl,array('extension'=> $extensionsArr[0]));
            return $output;

            ..and I get this HTML:
            <li>avi,flv,wmv,mov,mp4,mpg,mpeg,bmp,gif,jpg,jpeg,pdf,png,tiff</li>


            Apparently, print_r doesn't work if the array is only one value? And not sure what to make of the fact that the comma-separated string does not explode into a multi-valued array.

            Karl

              • 3749
              • 24,544 Posts
              The only possible explanation I can think of for what you're seeing is that either the commas in your extensions are not really commas, or the comma delimiter in your code has a space inside the quotes.

              The only way explode() returns a one-element array is if the delimiter is not found. I guess it could be a character set mismatch, but it seems very unlikely.

              Try this code to see what happens:

              $extensions = $modx->getOption('upload_files', null, array());
              $extensionsArr = explode(',',$extensions);
              $modx->log(modX::LOG_LEVEL_ERROR, print_r($extensionArr, true));
              return '<pre>' . print_r($extensionArr, true) . '</pre>';
              


              If you don't see the array, check the Error log.
                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
                • 49659
                • 86 Posts
                Hi Bob,

                I ran that code. This line:
                $modx->log(modX::LOG_LEVEL_ERROR, print_r($extensionArr, true));

                ..generates this output in the error log:
                [2018-12-10 00:24:13] (ERROR @ /paas/c0651/www/core/cache/includes/elements/modsnippet/30.include.cache.php : 32)


                And this line:
                return '<pre>' . print_r($extensionArr, true) . '</pre>';

                ..as before, generates this HTML:
                <pre></pre>


                Karl
                  • 3749
                  • 24,544 Posts
                  You've clearly entered some kind of alternate universe. wink

                  Try just this line:

                  return 'FILES: ' .  $modx->getOption('upload_files', null, array());


                  And check to make sure the upload_files System Setting is not empty.
                    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
                    • 49659
                    • 86 Posts
                    Hi Bob,

                    This line:
                    return 'FILES: ' .  $modx->getOption('upload_files', null, array());

                    ..results in this returned:
                    FILES: txt,html,htm,xml,js,css,zip,gz,rar,z,tgz,tar,mp3,mp4,aac,wav,au,wmv,avi,mpg,mpeg,pdf,doc,docx,xls,xlsx,ppt,pptx,jpg,jpeg,png,tiff,svg,svgz,gif,psd,ico,bmp,odt,ods,odp,odb,odg,odf,md,ttf,woff,eot,scss,less,map
                    


                    This is the value that exists in the 'upload_files' system settings.

                    Karl