簡體   English   中英

如何使用PHP readdir排除某些文件類型?

[英]How to exclude certain file types with PHP readdir?

我正在使用一個php掃描目錄腳本,它將掃描目錄的內容,然后使用指向目錄內容的鏈接填充頁面。

<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {$count++;
        print("<a href=\"".$file."\">".$file."</a><br />\n");
    }
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?

我想知道如何排除某些文件或文件類型,如test.xml,durka.xslt或.html顯示在填充頁面上。 我有一些代碼,但不知道如何集成它。 任何幫助將非常感謝...

?php
if ($handle = opendir(‘.’)) {
while (false !== ($file = readdir($handle)))
{
if ($file != “.” && $file != “..”
&& $file != “NOT-TO-BE-IN-1.php”
&& $file != “NOT-TO-BE-IN-2.html”
&& $file != “NOT-TO-BE-IN-3.jpg”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”

)
<?php

  // These files will be ignored
  $excludedFiles = array (
    'excludeMe.file',
    'excludeMeAs.well'
  );

  // These file extensions will be ignored
  $excludedExtensions = array (
    'html',
    'htm',
    'php'
  );

  // Make sure we ignore . and ..
  $excludedFiles = array_merge($excludedFiles,array('.','..')); 

  // Convert to lower case so we are not case-sensitive
  for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
  for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);

  // Loop through directory
  $count = 0;
  if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
      $extn = explode('.',$file);
      $extn = array_pop($extn);
      // Only echo links for files that don't match our rules
      if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)) {
        $count++;
        print("<a href=\"".$file."\">".$file."</a><br />\n");
      }
    }
    echo '<br /><br /><a href="..">Return</a>';
    closedir($handle);
  }

?>
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != ".." 
    && $file != "NOT-TO-BE-IN-1.php"
    && $file != "NOT-TO-BE-IN-2.html"
    && $file != "NOT-TO-BE-IN-3.jpg"
    && substr($file,-strlen(".html")) != ".html" //if you don't want to include .html files, for instance
    && substr($file,-strlen(".js")) != ".js" //if you don't want to include .js files, for instance
    && $file != ""
    ) {$count++;
        print("<a href=\"".$file."\">".$file."</a><br />\n");
    }
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?>

另一種方式:

$excludeExtensions = array(
    'php',
    'html',
    'jpg'
);

if ($file != "." && $file != ".." && !in_array(pathinfo($file, PATHINFO_EXTENSION), $excludeExtensions))

編輯:我太晚了:)

<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".."
&& $file != "NOT-TO-BE-IN-1.php"
&& $file != "NOT-TO-BE-IN-2.html"
&& $file != "NOT-TO-BE-IN-3.jpg"
&& $file != ""
&& $file != ""
&& $file != ""
&& $file != ""
&& $file != ""
)
    echo file_get_contents($file);

將顯示所有沒有名字的頁面的內容

  • NOT-TO-BE-IN-1.PHP
  • NOT-TO-BE-IN-2.HTML
  • NOT-TO-BE-IN-3.JPG

你可以限制某些文件路徑

if (!preg_match('/(php|html|jpg)/', $file))

你也可以使用glob

foreach (glob("*.{php|html|jpg}",GLOB_BRACE) as $file) {
    echo file_get_contents($file);
}

有關詳細信息,請參閱http://php.net/manual/en/function.glob.php

嘗試類似的東西:

$excluded_files = array('test.xslt');
$excluded_ext   = array('html');

while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != ".." &&
        !in_array($file, $excluded_files) &&
        !in_array(pathinfo($file, PATHINFO_EXTENSION), $excluded_ext)) 
    {
        // do stuff
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM