简体   繁体   English

当路径在PHP中包含空格时,scandir或opendir不起作用

[英]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. 例如,在以下路径中,目录名称为“Stack Overflow_files”,其中包含空格。

 $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' . 我可以从终端运行ls'$ dir'找到解决方案。 but is there any way in php to get the dirctory listing? 但有没有办法在PHP中获取目录列表?

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. 尝试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"; $ dir =“带空格的文件夹”;
$files=scandir("${path}/${dir}"); $文件= SCANDIR( “$ {路径} / $ {DIR}”);

And bingo 和宾果游戏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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