Support

Account

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

Solved

how to get filetype (extension)?

  • i have a custom field code in my template where i get the filesize, any idea how to get the filetype, too? (file extension).

    <?php while(the_flexible_field("downloads")): ?>
    <?php if(get_row_layout() == 'flexibel_download'): // layout: Content ?>  
    <?php $attachment_id = get_sub_field('datei');
    	$url = wp_get_attachment_url( $attachment_id );
    	$title = get_the_title( $attachment_id );
    	// part where to get the filesize
    	$filesize = filesize( get_attached_file( $attachment_id ) );
    	$filesize = size_format($filesize, 2);	?>
    
    <li class="cf dlbox">
    <a href="<?php echo $url; ?>" class="cf"><?php echo $title; ?><small><strong>Dateigröße:</strong>&nbsp;<em>(<?php echo $filesize; ?>)</em></small></a>
    </li>
    <?php endif; endwhile; ?> 
  • Hi there, what you’re trying to do could be achieved with a built-in PHP function.

    Check out this article:

    http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php

    You would declare a variable to use as the file extension. Something like:

    $ext = pathinfo($url, PATHINFO_EXTENSION);

    Reference: http://php.net/manual/en/function.pathinfo.php

  • Hi @herrfischer, @smspaul’s answer should do the trick. 🙂

  • great, thanks that works 🙂

    <?php if(get_field("downloads")): ?>
    <div id="downloadbox">
    <h3>Files</h3>
    <ul class="downloads">
    <?php while(the_flexible_field("downloads")): ?>
    <?php if(get_row_layout() == 'flexible_download'): // layout: Content ?>  
    <?php $attachment_id = get_sub_field('file');
    	$url = wp_get_attachment_url( $attachment_id );
    	$title = get_the_title( $attachment_id );
    	// part where to get the filesize
    	$filesize = filesize( get_attached_file( $attachment_id ) );
    	$filesize = size_format($filesize, 2);		
    
    	// part where to get the extension
    	$path_info = pathinfo( get_attached_file( $attachment_id ) );?>
    <li class="cf dlbox">
    	<a href="<?php echo $url; ?>" class="cf">
    	<span class="dl-title"><?php echo $title; ?><small><nobr><strong>Size:</strong>&nbsp;<em>(<?php echo $filesize; ?>)</em></nobr></small></span>
    <span class="dl-extension"><span class="dl-extension-inner"><?php echo $path_info['extension']; ?></span></span>
    </a>
    </li>
    <?php endif; endwhile; ?> 
    </ul>
    </div>
    <?php endif; ?>
  • This worked for me and I am able to get the file type extension but now I would like to use an if statement with that extension so I can assign icons to my files based on the file type. So for example, if the filetype equals pdf then I would output a pdf icon. How can I do an if else type of statement using this?

    I tried the following but no luck:

    <?php $attachment_id = get_field('document_upload');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    
    // part where to get the filesize
    $filesize = filesize( get_attached_file( $attachment_id ) );
    $filesize = size_format($filesize, 2);
    $path_info = pathinfo( get_attached_file( $attachment_id ) );
    
    // show custom field
    if (get_field('document_upload')): ?>
    
    <?php echo $filesize; ?>
    <?php echo $path_info['extension']; ?>
    
    <?php
    if ($path_info = "pdf") {
        echo "<i class='fa fa-file-pdf-o' aria-hidden='true'></i>";
    } ?>
    
    <?php endif; ?>	

    Not really sure where to go from here. Thanks

  • 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; ?>
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘how to get filetype (extension)?’ is closed to new replies.