如何逐个获取目录中的文件链接以使用它们?

时间:2017-05-08 04:22:02

标签: javascript php html

<div class="video" data-tags="Awesome" data-video-url="LINK OF FIRST VIDEO FILE IN THE DIRECTORY /VIDEOS WITH A PREVIEW IMAGE">

<div class="video" data-tags="Awesome" data-video-url="LINK OF SECOND VIDEO FILE IN THE DIRECTORY /VIDEOS WITH A PREVIEW IMAGE">

如上面的代码所示,我需要能够破坏导演中文件并逐个生成项目链接的东西。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

<?php
function dirToArray($dir) {
   $result = array();
   $cdir = scandir($dir);
   foreach ($cdir as $key => $value)
   {
      if (!in_array($value,array(".","..")))
      {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
         {
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
         }
         else
         {
            $result[] = $value;
         }
      }
   }

   return $result;
}

$files = dirToArray("/home/user/folder")
foreach ($files as $file) {
   echo "<div class='video' data-tags='Awesome' data-video-url='$file'>";
}
?>