Scandir or opendir does not work when the path contains space in PHP

The following functions do not work when there is a space in the directory path. For example, the following path name indicates "Stack Overflow_files", which contains a space.

 $dir = '/home/mamun/workspace/barj/barj/barj/Sequence/Stack Overflow_files';
 $scanned_directory =  array_diff(scandir($dir, 0), array('..', '.'))

or

$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
         $scanned_directory[] = $filename;
}

I can find a solution from a terminal running ls '$ dir'. but is there any way in php to get a dirctory list?

+3
source share
5 answers

try performing a backslash space -

$dir = '/home/mamun/workspace/barj/barj/barj/Sequence/Stack\ Overflow_files';
0
source

Try urlencoding. Directory path May be like

         $dir = '/home/mamun/workspace/barj/barj/barj/Sequence/Stack%20Overflow_files';
0
source

Try

$dir = urlencode('/home/mamun/workspace/barj/barj/barj/Sequence/Stack Overflow_files');
0

Bro

$ PATH = "/Var/WWW/";
$ dir = " ",
$ = ScanDir ( "$ {}/${}" );

0

Alternative solution

 $URL = str_replace("\ ","%20","../pasta1/pasta com espaco/pasta2");<br>
 $arquivos = scandir($URL);

<br>

 echo $arquiovos[0];<br>
 echo $arquiovos[1];<br>
-1
source

All Articles