PHP is_file() 无法正常工作

标签 php

我想检查目录中的文件,我正在使用 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;
  }
}

使用 $file 变量的 scandir() 的结果:

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

$sfiles 的结果:

Array
(
)

最佳答案

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

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

关于PHP is_file() 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117361/

相关文章:

php - 即使从数据库中获取值后,在 codeigniter 3 中获取 undefined index 返回日期?

php - .html 与 .php |区别,当我在两种扩展类型中编写 php 时

php - 具有 3 种不同背景的交替行颜色

php - 为什么 unset() 在 PHP 三元运算符中不起作用

php - 在 PHP 中更改父属性的值

php - 如何将参数绑定(bind)到模型上使用的 Laravel 中的原始数据库查询?

php - 订阅包并存储它们

php - 从 PHP 执行 scanimage 命令

php - 使用php从数据库检索数据到android

javascript - 如何从 wordpress 页面上传 pdf 文件,文件必须保存在 wp-content 上传目录文件夹中