简体   繁体   English

使用opendir / readdir函数后重复文本

[英]repeating text after using opendir/readdir functions

its a mystery to me why this is happening. 为什么发生这种情况对我来说还是个谜。 so here is the code: 所以这是代码:

$dir = 'test/';
$de = opendir($dir);
if ($de) {
    while (($file = readdir($de)) !== false) {
        $path = $dir . $file;
        $file_title = 'this text gets repeated three times, each followed by a dot' . $file;
        echo $file;
    }
} else {
    echo "invalid directory";
}

so If $file= video.mp4 and $file_title='file name'.$file; 因此,如果$ file = video.mp4和$ file_title ='文件名'。$ file; it would look like this: 它看起来像这样:

'file name.file name.file namevideo.mp4' and if there were no string before the variable, ie $file_title=$title there would be three dots before the variable like this: ...video.mp4 'file name.file name.file namevideo.mp4',如果变量前没有字符串,即$ file_title = $ title,则变量前会有三个点,如下所示:... video.mp4

if anyone has any idea what's happening please let me know. 如果有人知道发生了什么,请告诉我。 Thanks. 谢谢。

You get repeating text because your code contains a loop. 您会重复文本,因为您的代码包含一个循环。 That's what while does: it runs code repeatedly. 那就是while做什么:它反复运行代码。

You are listing the . 您正在列出. and .. directories, use is_file() to check so the entry is really a file and not a directory: ..目录,请使用is_file()进行检查,以使该条目实际上是文件而不是目录:

if (is_file($dir . $file)) {
   // entry is a file
}

Here you can read more about the dot directories: There's a lot in the dot 在这里,您可以阅读有关dot目录的更多信息: dot 有很多内容

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

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