【问题标题】:I have a problem with spaces in folder name我对文件夹名称中的空格有疑问
【发布时间】:2021-02-12 21:08:54
【问题描述】:

我正在使用以下函数,在给定文件夹和所有子文件夹中查找具有特定文件扩展名的文件:

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 是一个以avimkvmp4 为值的数组。

$startfolder 是一个以/mnt/HD1_4TB/refazer/A View to a Kill 为值的字符串。

当我尝试运行该函数时,它会抛出一个错误消息: failed to open dir: No such file or directory.

最奇怪的是,如果我将$starfolder 变量更改为/mnt/HD1_4TB/refazer,该函数将起作用并返回文件夹“A View to a Kill”中的文件(即使文件夹名称中有空格)。 如果我将文件夹“A View to a Kill”重命名为“AViewtoaKill”并将$startfolder 变量更改为/mnt/HD1_4TB/refazer/AViewtoaKill,该功能也可以工作..... 所以.....我认为我的问题是文件夹名称中的空格。 有人可以帮我解决这个问题而不删除文件夹名称中的空格吗?

谢谢

【问题讨论】:

  • 好电影,好音乐
  • 我建议查看A View to a Kill 并检查它是否确实具有您认为的空格字节序列。例如,它可能有一个隐藏的尾随空格或其他类似空格的字符?从/mnt/HD1_4TB/refazer 开始,只需将内容列为hex or bytes
  • @ChrisHaas 按照您的提示,我得到:/mnt/HD1_4TB/refazer/A View to a Kill array (size=41) ..... 22 => int 65 23 => int 194 24 => int 160 25 => int 86 26 => int 105 27 => int 101 28 => int 119 29 => int 194 30 => int 160 31 => int 116 32 => int 111 33 => int 194 34 => int 160 35 => int 97 36 => int 194 37 => int 160 38 => int 75 39 => int 105 40 => int 108 41 => int 108 空格字符不应该是“32 “?但在这里它显示为“194”“160”......我开始认为我的问题与php无关,而是与debian有关。
  • @ChrisHaas .... 感谢您的想法。您对我如何解决我的问题有任何想法吗?
  • 找到了.... : $startfolder=str_replace(chr(194) . chr(160), chr(32), $startfolder);

标签: php linux debian-stretch


【解决方案1】:

使用 Explode 和 Implode 函数进行成功搜索,例如从目录中获取文件夹名称并从其名称中删除所有空格并进行比较,在后端使用不带空格的文件夹名称。试试这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多