0

I've a folder on server, m fetching those files but getting some difficulties. my code is

$dir = 'F:\\wamp\www\files';
if (is_dir($dir)) {
if(($file = scandir($dir))!==false) {    

    foreach ($file as $key => $value) {                  
        $name = substr($value,0,-7);   

the scandir() function retuns array of files which returns list of names, i.e.

manoj254.txt
manoj234.txt
pooza145.txt
pooza452.txt
ramesh418.txt
ramesh231.txt  

and i've remove the extension by using substr() function after this the output is $name

manoj
manoj
pooza
pooza
ramesh
ramesh

But i need the output $name, without duplicate.i.e

manoj
pooza
ramesh

which means remove duplicates... can any body help me plz !!

Manu Bist
  • 1
  • 1

1 Answers1

0

You may try something like this:

function getUniqueFileNames($path = null)
{
    if($path && $files = scandir($path)) { 
        $names = array_map(function($i) {
            if(is_file($i)) return str_replace(strrchr($i, '.'), '', $i);
        }, $files);
        return array_unique(array_filter($names));
    }
}

Use it like:

$filenames = getUniqueFileNames('F:\\wamp\www\files');

There are other ways though.

The Alpha
  • 143,660
  • 29
  • 287
  • 307