【问题标题】:PHP is_file() not work correctlyPHP is_file() 无法正常工作
【发布时间】:2014-06-09 17:34:57
【问题描述】:

我想检查目录中的文件,我正在使用scandir()和is_file() 进行检查。在这个简单的代码is_file() 中为我返回false。但在目录中我有 3 个文件。

$files = scandir('uploads/slideShow');
$sfiles = array();
foreach($files as $file) {
  if( is_file($file) ) {
    $sfiles[] = $file;
    // echo $file;
  }
}

scandir() 与 $files 变量的结果:

Array
(
  [0] => .
  [1] => ..
  [2] => 6696_930.jpg
  [3] => 9_8912141076_L600.jpg
  [4] => untitled file.txt
)

$sfiles 的结果:

Array
(
)

【问题讨论】:

    标签: php


    【解决方案1】:

    问题在于scandir 只返回文件名,因此您的代码在当前 目录中寻找untitled file.txt 等人,而不是扫描的目录。

    这与glob("uploads/slideShow/*") 不同,后者将返回您正在查看的文件的完整路径。如果您将此glob 与is_file 一起使用,它应该可以正常工作。

    【讨论】:

      【解决方案2】:

      这是因为您检查它是否是当前目录中的文件,而文件位于另一个目录中...试试这个:

      $files = scandir('uploads/slideShow');
      // ...
      foreach($files as $file) {
          if ( is_file('uploads/slideShow/'. $file) ) {
              // file found!
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-20
        • 2015-11-15
        • 1970-01-01
        • 2019-01-13
        • 2021-08-16
        • 2012-10-28
        • 2013-10-13
        • 2012-12-06
        • 1970-01-01
        相关资源
        最近更新 更多