DolDurma
DolDurma

Reputation: 17321

PHP is_file() not work correctly

I want to check files in directory and i'm using scandir()and is_file() for check. In this simple code is_file() return false for me. but in directory I have 3 file.

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

result for scandir() with $files variable:

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

result for $sfiles:

Array
(
)

Upvotes: 4

Views: 4128

Answers (2)

giorgio
giorgio

Reputation: 10212

that's beacuse you check if it's a file in the current dir, while the files reside in another dir... Try this:

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

Upvotes: 5

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

The problem is that scandir returns just the filename, so your code is looking for untitled file.txt et al in the current directory, not the scanned one.

This is in contrast to glob("uploads/slideShow/*"), which will returns the full path to the files you're looking at. If you use this glob with is_file, it should work fine.

Upvotes: 6

Related Questions