简体   繁体   中英

scandir or opendir does not work when path contains space in PHP

The following functions does not work when there is space in the directory path. For example, in the following path directory name is '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 the solution from the terminal running ls '$dir' . but is there any way in php to get the dirctory listing?

Alternative solution

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

<br>

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

尝试用反斜杠逃离空间 -

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

Try urlencoding. The directory path May be like

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

尝试

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

Bro this is the final solution

$path="/var/www/images";
$dir="folder with space";
$files=scandir("${path}/${dir}");

And bingo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM