I have a problem with spaces in folder name

Viewed 118

I'm using the following function, to find files with specific file extensions within a given folder and all subfolders:

function FindVideoFiles($startfolder,$specificextensions){
        $it = new RecursiveDirectoryIterator($startfolder);
        foreach( new RecursiveIteratorIterator($it) as $file) {
            if (in_array(strtoupper(substr($file, strrpos($file, '.') + 1)), $specificextensions)) {
                $files[]=array('nomedapasta'=>pathinfo($file,PATHINFO_DIRNAME));
            }
        }
        return $files;
}

$specificextensions is an array with avi,mkv and mp4 as values.

$startfolder is a string with /mnt/HD1_4TB/refazer/A View to a Kill as value.

When I try to run the function, it throws me an error saying: failed to open dir: No such file or directory.

The weirdest thing is, if I change the $starfolder variable to /mnt/HD1_4TB/refazer, the function works and return the files inside the folder "A View to a Kill" (even with spaces in folder name). If i rename the folder "A View to a Kill" to "AViewtoaKill" and change the $startfolder variable to /mnt/HD1_4TB/refazer/AViewtoaKill, the function also work..... so..... I think that my problem is the spaces in the folder name. Can someone help me on how I can solve this problem without removing the spaces in the folder name?

Thanks

1 Answers

Use Explode and Implode function to do a successful search, example get the folder name from directory and remove all the spaces from it's name and compare, use folders name at backend without spaces. Try this.

$fileNameWithSpace = "A View to a Kill";
$fileWithoutSpace = (explode(" ",$fileNameWithSpace ));
echo implode("",$strWithoutSpace); //AViewtoaKill
Related