【问题标题】:Search for pdf files搜索 pdf 文件
【发布时间】:2019-08-22 09:54:02
【问题描述】:

我有一个小问题,这是我列出文件夹和子文件夹中所有文件的代码;

if ($handle = opendir($dir)) {

$allFiles = array();

while (false !== ($entry = readdir($handle))) {
    if ($entry != "." && $entry != "..") {
        if (is_dir($dir . "/" . $entry)){
            $allFiles[] = "D: " . $dir . "/" . $entry;
        }else{
            $extension = strtoupper(pathinfo($entry, PATHINFO_EXTENSION));
            $fileNoExten = pathinfo($entry, PATHINFO_FILENAME);
            $directory = substr(str_replace('/', ' > ', $dir), $rootLenOnce + 3);
            $listagem .= '<tr>';
                $listagem .= "<td><a href='../" . $dir . "/" . $entry . "' ' target='_blank'>" . $entry . "</a></td>";
                //$listagem .= "<td><small>" . $directory . "</small></td>";
                $listagem .= "<td>" . $extension . "</td>";
                $listagem .= "<td><a class='download-cell' href='../".$dir ."/". $entry."' ' download> <i class='fa fa-download' ></i></a></td>";
                $listagem .= "<td class='display-none'>" . $fileNoExten . "</td>";
                $allFiles[] = "F: " . $dir . "/" . $entry;
            $listagem .= '</tr>';
            echo "<pre>"; print_r(glob("*.pdf")); echo "</pre>";
        }
    }
}
closedir($handle);

foreach($allFiles as $value){
    $displayName = substr($value, $rootLen + 4);
    $fileName    = substr($value, 3);
    $linkName    = str_replace(" ", "%20", substr($value, $pathLen + 3));
    if (is_dir($fileName)) {
        myScanDirPdf($fileName, $level + 1, strlen($fileName),$rootLenOnce);
     }
   }
 }
 return $listagem;
}

我需要过滤搜索,只搜索 .pdf 文件。

谁能帮帮我!

谢谢!

我尝试使用 glob 函数,但效果不佳。

谢谢!

【问题讨论】:

  • @AndreaManzi 谢谢!但不工作。 :(
  • 您确定 $dir 是您目录的完整路径吗?
  • @AndreaManzi 是的
  • 网络服务器在 $dir 文件夹中有读取权限吗?

标签: php foreach glob readdir opendir


【解决方案1】:

当你遍历每个文件时,你可以使用

        if(stripos($fileName, ".pdf"))

希望这会有所帮助

在列出所有文件和子文件夹时,我还有一个建议。 您可以使用递归迭代器

$folderName = $_POST['folderName'];
$dir = new RecursiveDirectoryIterator($folderName);
$it  = new RecursiveIteratorIterator($dir);
foreach ($it as $fileinfo) {
    if ($fileinfo->isDir()) {
    }elseif ($fileinfo->isFile()) {
        $fileName = $fileinfo->getFileName();
        if(stripos($fileName, ".pdf")) {
        //do what you need to do 
        }
    }
}

【讨论】:

  • 当我尝试使用“stripos”时,只显示根污垢中的文件。谢谢!
  • @freixo,我用这种方法遍历文件夹和子文件夹来复制所有的pdf文件。它对我有用
  • 再试试看,如果可行,请接受我的回答。谢谢
  • 好吧,有个小问题,在我的代码中,我可以使用 stripos 吗?谢谢!
  • 是的,你可以,当你有一个数组中的所有文件时,循环遍历每个文件并根据区分大小写使用stripos或strpos。
猜你喜欢
  • 2023-03-18
  • 2022-08-02
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 2011-08-23
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多