Support

Account

Home Forums General Issues how to get filetype (extension)? Reply To: how to get filetype (extension)?

  • Here’s how I’ve done it. I haven’t tested uploading every file type but the ones I have tried do work. This is using material design icons.

    
    <?php if( have_rows( 'download_list_clone_download_groups' ) ): ?>
      <section class="download-lists">
        <?php while( have_rows( 'download_list_clone_download_groups' ) ): the_row(); ?>
          <article class="download-list">
            <h3 class="download-list__title"><?php the_sub_field( 'group_title' ); ?></h3>
            <?php if( have_rows( 'downloads' ) ): ?>
              <ul>
                <?php while( have_rows( 'downloads' ) ): the_row(); ?>
                  <?php $attachment_id = get_sub_field('file'); ?>
                  <?php $attachment_url = wp_get_attachment_url($attachment_id); ?>
                  <?php $pathinfo = pathinfo( get_attached_file($attachment_id)); ?>
                  <?php $filetype = $pathinfo['extension']; ?>
                  <?php
                    switch ($filetype) {
                      case "pdf":
                          $fileclass = 'file-pdf';
                          break;
                      case "jpg":
                      case "jpeg":
                      case "png":
                      case "svg":
                          $fileclass = 'file-image';
                          break;
                      case "pptx":
                      case "pptm":
                      case "ppt":
                          $fileclass = 'file-powerpoint';
                          break;
                      case "xlsx":
                      case "xlsm":
                      case "xls":
                          $fileclass = 'file-excel';
                          break;
                      case "doc":
                      case "docm":
                      case "docx":
                      case "odt":
                      case "rtf":
                      case "txt":
                          $fileclass = 'file-word';
                          break;
                      default:
                          $fileclass = 'file-document';
                          break;
                    }
                   ?>
                  <li class="download-list__download"><a href="<?php echo $attachment_url; ?>"><i class="mdi mdi-<?php echo $fileclass; ?>"></i><span><?php the_sub_field( 'download_title' ); ?></span><i class="mdi mdi-download"></i></a></li>
                <?php endwhile; ?>
              </ul>
            <?php endif; ?>
          </article>
        <?php endwhile; ?>
      </section>
    
    <?php endif; ?>