Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP is_file() not work correctly

Tags:

php

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
(
)
like image 448
DolDurma Avatar asked Dec 20 '22 14:12

DolDurma


2 Answers

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.

like image 50
Niet the Dark Absol Avatar answered Dec 31 '22 20:12

Niet the Dark Absol


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!
    }
}
like image 44
giorgio Avatar answered Dec 31 '22 20:12

giorgio