Freelancer's Playground! New knowledges every now and then

11Jun/0718

Get Filename Without Extension

If you want to get the filename without extension in PHP, you can use this function :

<?php
function getFilenameWithoutExt($filename){
    $pos = strripos($filename, '.');
    if($pos === false){
        return $filename;
    }else{
        return substr($filename, 0, $pos);
    }
}
?>


Here's example on how to use it :

<?php
echo getFilenameWithoutExt('this_is_a_file.with_dot_on_it.txt');
//should generate output `this_is_a_file.with_dot_on_it`
?>

  • http://dollyaswin.net/ Dolly Aswin Hrp

    Pake basename() aja om.

  • http://dollyaswin.net Dolly Aswin Hrp

    Pake basename() aja om.

  • http://bayu.freelancer.web.id/ Arief Bayu Purwanto

    hmm, maybe the basename() could be implemented in the first line of the function, so the revision look like this :

    &lt;?php
    function getFilenameWithoutExt($filename){
        $filename = basename($filename);
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    ?&gt;
    

    because this script is used to get the filename without the extension, where the extension is now known yet.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    hmm, maybe the basename() could be implemented in the first line of the function, so the revision look like this :

    <?php
    function getFilenameWithoutExt($filename){
        $filename = basename($filename);
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    ?>
    

    because this script is used to get the filename without the extension, where the extension is now known yet.

  • dcky

    Sorry, i don’t have experience with PHP, but how if the filename itself contain dot(ex:myfile.1-0.zip)?

  • dcky

    Sorry, i don’t have experience with PHP, but how if the filename itself contain dot(ex:myfile.1-0.zip)?

  • http://bayu.freelancer.web.id/ Arief Bayu Purwanto

    ah, you’ve missed my example then, please take a look again, you’ll see that my example is using [dot] on filename itself.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    ah, you’ve missed my example then, please take a look again, you’ll see that my example is using [dot] on filename itself.

  • Diblue

    more simple way :

    function getFilenameWithoutExt($filename)
    {
    $exp = explode(‘.’,$filename);
    array_pop($exp);
    return $exp;
    }

    :)

  • Diblue

    more simple way :

    function getFilenameWithoutExt($filename)
    {
    $exp = explode(‘.’,$filename);
    array_pop($exp);
    return $exp;
    }

    :)

  • Diblue

    sorry kelupaan :

    function getFilenameWithoutExt($filename)
    {
    $exp = explode(‘.’,$filename);
    array_pop($exp);
    return implode(‘.’,$exp);
    }

  • Diblue

    sorry kelupaan :

    function getFilenameWithoutExt($filename)
    {
    $exp = explode(‘.’,$filename);
    array_pop($exp);
    return implode(‘.’,$exp);
    }

  • http://bayu.freelancer.web.id/ Arief Bayu Purwanto

    hmm… saya tes ternyata diretory file tersebut masih ada.

    &lt;?php
    
    function getFilenameWithoutExt2($filename){
        $filename = basename($filename);
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    
    function getFilenameWithoutExt($filename){
        $exp = explode('.',$filename);
        array_pop($exp);
        return implode('.',$exp);
    }
    
    $filename = '/var/www/ini.dimana.cocok.avi';
    echo getFilenameWithoutExt($filename) . '&lt;br  /&gt;';///var/www/ini.dimana.cocok
    echo getFilenameWithoutExt2($filename);//ini.dimana.cocok
    ?&gt;
    

    masuk juga sih code-nya cuman kurang basename() aja.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    hmm… saya tes ternyata diretory file tersebut masih ada.

    <?php
    
    function getFilenameWithoutExt2($filename){
        $filename = basename($filename);
        $pos = strripos($filename, '.');
        if($pos === false){
            return $filename;
        }else{
            return substr($filename, 0, $pos);
        }
    }
    
    function getFilenameWithoutExt($filename){
        $exp = explode('.',$filename);
        array_pop($exp);
        return implode('.',$exp);
    }
    
    $filename = '/var/www/ini.dimana.cocok.avi';
    echo getFilenameWithoutExt($filename) . '<br />';///var/www/ini.dimana.cocok
    echo getFilenameWithoutExt2($filename);//ini.dimana.cocok
    ?>
    

    masuk juga sih code-nya cuman kurang basename() aja.

  • Diblue

    di edit jadi gini deh ya :

    function getFilenameWithoutExt($filename){
    $exp = explode(‘.’,$filename);
    array_pop($exp);
    return basename(implode(‘.’,$exp));
    }
    *tetep kekeuh 3 baris :p*

  • Diblue

    di edit jadi gini deh ya :

    function getFilenameWithoutExt($filename){
    $exp = explode(‘.’,$filename);
    array_pop($exp);
    return basename(implode(‘.’,$exp));
    }
    *tetep kekeuh 3 baris :p*

  • http://bayu.freelancer.web.id/ Arief Bayu Purwanto

    hiahahahahahaha.
    bagus juga masukannya :D.

  • http://bayu.freelancer.web.id Arief Bayu Purwanto

    hiahahahahahaha.
    bagus juga masukannya :D .