-1

My code

<?php
    $video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
    /** The line below is giving me what I need **/
    $video_thumb_extension_large = substr($video_thumb_large, strrpos($video_thumb_large, '.') + 1);
    echo $video_thumb_extension_large; /** Output: png **/
?>

There are other methods to get the file extension, for example this Stack Overflow question has some answers but my code was not available in the answers.

I'm wondering why my code is good or bad and why or why not to use my code on a production site. What is better to do in this case? I can also use explode() on the dot and use the last part in the array() but is it better?

What would be better or the best to get the file extension without the dot (.)?

Janis S.
  • 2,526
  • 22
  • 32
jagb
  • 912
  • 11
  • 26
  • 1
    If the code works and you just want to know if there is a better way, you should post it in [Code Review](http://codereview.stackexchange.com/) instead. – M. Eriksson Sep 11 '17 at 22:57
  • Your submitted code is better than using an array, as it doesn't require you to create a new array instance. This is most likely the most efficient way you can do it. If you have a file instance, you should use the built-in method as in the linked answer as it has most likely already cached it. – Vilsol Sep 11 '17 at 22:57
  • 2
    There are lots of ways to do this, but whenever there is specialized functionality like `pathinfo()` in the standard library for the very purpose you should use that. – Michael Jaros Sep 11 '17 at 23:04
  • @MichaelJaros Yes, I know, that's what I did first but sometimes (not always) with warnings from php, very strange, that's why I've been testing other methodes to get the extension from a file.. – jagb Sep 11 '17 at 23:44
  • @Vilsol Yes, that's why I don't use an array for this.. – jagb Sep 11 '17 at 23:45
  • If you get warnings when using built in functions, I would rather debug those instead of creating hacks. Just my opinion, though. – M. Eriksson Sep 11 '17 at 23:53
  • @MagnusEriksson The line where the error was found sometimes with some files is the same line where I used the `pathinfo()` function before, the error is only there with some filenames but not with Ajmal Praveen's answer wich is working for all of the files and file types and it's faster.. – jagb Sep 12 '17 at 00:14

2 Answers2

1

Recommended from my own experience, faster than Basename, explode or using your own Func.

Try to use the Default PHP Func its Cachable in All Opcaches..

Recoded just replace with your old Code and execute

<?php
//Recoded by Ajmal PraveeN
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
$path_parts = pathinfo($video_thumb_large);
//out the file name and file extension without dot
echo 'File Name :'; echo $path_parts['filename']; echo '<br>';
echo 'File Extension :'; echo $path_parts['extension']; echo '<br>';
?>
Pang
  • 9,564
  • 146
  • 81
  • 122
Ajmal PraveeN
  • 414
  • 8
  • 16
  • You might want to add _why_ this is a better way, since that's what the OP actually asked about. – M. Eriksson Sep 11 '17 at 23:40
  • Magnus, Im using non database website but serves static huge files its been Since Couple of yrs where i use the same Path info to output the file info its simply awesome in speed compared basename or own func, explode are waste **Recommend from my own experience** Pathinfo for speed and caching in Zend caches etc.. using PHP default func are better PHP doc mentioned that. – Ajmal PraveeN Sep 11 '17 at 23:45
  • 1
    @AjmalPraveen Thank you! This function with pathinfo is working in all cases, and it seems to be faster as well, thanks again! – jagb Sep 12 '17 at 00:07
  • 1
    Thank you Confirmation JAGB, because i recommend this from my experience self test Use a Opcache it will cache all your infos and will gain more speed . for caching i recommend zend cache or use caching on your own disk. – Ajmal PraveeN Sep 12 '17 at 00:10
  • @AjmalPraveen Your welcome, I do use Google's MOD_pagespeed, I cache files on disk and caching the pages for some time on local disk where possible.. Again it's more than twice as fast as my methode and always working as well, very good job! – jagb Sep 12 '17 at 00:21
  • Oh Cool... Good job :) – Ajmal PraveeN Sep 12 '17 at 00:32
0

I think the best way you could do this it extract the extension, then strip the period from the beginning, like so:

<?php
function get_file_extension($file_name) {
    return substr(strrchr($file_name,'.'),1);
}
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
/** The line below is giving me what I need **/
$video_thumb_extension_large = get_file_extension($video_thumb_large);
echo $video_thumb_extension_large; /** Output: png **/
?>
  • Why is this better than the answer in the post the OP linked to in the question? (using PHP's built in `pathinfo($filename, PATHINFO_EXTENSION);`) – M. Eriksson Sep 11 '17 at 23:42
  • I've had problems in the past with this because of PHP versions, so I prefer to be sure it works in any version, which this should. – Joshua Anderson Sep 11 '17 at 23:45
  • `pathinfo()` has been around since PHP 4.0.3, so I doubt that would be an issue. We should also stop using hacks to support 10-15 year old versions at some point. – M. Eriksson Sep 11 '17 at 23:46