WarpConduit Computing

  • Quick Tips
  • Web Design & Development
  • Graphic Design
  • Home
  • WordPress Plugins
  • Password Generator
  • About
  • Contact

Get File Extension with PHP + Benchmark Results

February 27, 2011 by Josh Hartman

I know you’ve probably seen this topic a hundred times, and so have I, but this one has a different twist.  We already know that there are many ways to get the extension of a file, but which one is the fastest?  That’s what I’m going to address right now.

Contenders

In each of the code examples $file is set to c:\\xampplite\\htdocs\\index.php.

String-to-Array Method

<?php
$ext = end(explode('.', $file));
echo $ext; // outputs 'php'
?>

<?php $ext = end(explode('.', $file)); echo $ext; // outputs 'php' ?>

Sub-String Method

<?php
$ext = substr($file, strrpos($file, '.')+1);
echo $ext; // outputs 'php'
?>

<?php $ext = substr($file, strrpos($file, '.')+1); echo $ext; // outputs 'php' ?>

Path Info Method

<?php
$ext = pathinfo($file, PATHINFO_EXTENSION);
echo $ext; // outputs 'php'
?>

<?php $ext = pathinfo($file, PATHINFO_EXTENSION); echo $ext; // outputs 'php' ?>

Setup

To test each of the contenders I put together a script that timed the execution of 1,000,000 iterations of each command. If you would like the script you can download it here.

Results

Get File Extension PHP Benchmark
Sub-String Method: 0.778156 seconds
String-to-Array Method: 1.889744 seconds
Path Info Method: 2.020036 seconds

Winner

Our winner? The Sub-String Method! Next time you reach for that line of code to get a file’s extension, go for gold, and choose the Sub-String Method.

<?php
$ext = substr($file, strrpos($file, '.')+1);
echo $ext; // outputs 'php'
?>

<?php $ext = substr($file, strrpos($file, '.')+1); echo $ext; // outputs 'php' ?>

Filed Under: Web Design & Development Tagged With: benchmark, extension, file, php

Comments

  1. Stan says

    August 9, 2011 at 5:47 PM

    What if the filename is index.bak.php

    • Josh Hartman says

      August 9, 2011 at 11:57 PM

      Well, the extension of that file would be php, so it would return php.

  2. Martin says

    January 2, 2012 at 8:11 PM

    Sub-String Method

    if $file=’noextension’, $ext will be substr($file, 1), this does not work well
    so things go like:

    • Josh Hartman says

      January 3, 2012 at 11:19 AM

      Yes, if you know that you are working with file names that have no extension then modify it like so:

      <?php
      $file = 'noextension';
      $ext = (strrpos($file, '.')!==false) ? substr($file, strrpos($file, '.')+1) : '';
      echo $ext;
      ?>

      <?php $file = 'noextension'; $ext = (strrpos($file, '.')!==false) ? substr($file, strrpos($file, '.')+1) : ''; echo $ext; ?>

Connect

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • YouTube

Recent Posts

  • Extremely Useful Applications for Web Development and IT Tasks
  • Installing BookStack Wiki on cPanel Shared Hosting
  • Media (MIME) Type Reference List

Tags

automatic base64 benchmark cache counter css deflate email font gzip htaccess html image inarray increment inline images in_array javascript jquery link list magento mailto menu metadot mysql number obfuscation opencart operating system order php random redirect rewriterule slashes software timestamp ubuntu unix upgrade url windows windows 7 wordpress

Blogroll

  • CodeIgniter
  • Fusion Forward
  • jQuery
  • Nettuts+
  • Smashing Magazine

© 2021 WarpConduit Computing. All Rights Reserved.