Grab file name that does not have extension using foreach (glob())

Asked

Viewed 212 times

2

How to Get Only Files without Extension?

examples

1bdfe4baf9061c3667ded70d8f66142c

2a0daf2d8d5b7ea1813c7a84b146d092

91e3b288eab8d59598a52221296f8995

 $f=glob("*");
 foreach ($f as $arquivo) {
 echo "$arquivo<br>";
 }
 //Neste caso esta exibindo todos arquivos

1 answer

3


You can use:

  • strpos to find out if the name has dot (.)
  • array_filter to filter the array before iterating in the foreach
  • basename to get only the file name

It would look something like:

<?php

$arquivos = array_filter(glob('*'), function ($path) {
    return strpos(basename($path), '.') === false;
});

foreach ($arquivos as $arquivo) {
    echo "$arquivo<br>";
}

Got it wrong, I removed this part:

You can use the pathinfo thus:

<?php

$arquivos = glob("*");

foreach ($arquivos as $arquivo) {
    $arquivo = pathinfo($arquivo, PATHINFO_FILENAME);

    echo "$arquivo<br>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.