PHP is_file () not working properly

I want to check the files in the directory, and I use scandir()and is_file()to test. In this simple code, is_file()return falsefor me. but in the directory I have 3 files.

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

for scandir()with variable $file:

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

result for $sfiles:

Array
(
)
+4
source share
2 answers

The problem is that it scandironly returns the file name, so your code looks for untitled file.txtet al in the current directory, not the scanned one.

This contrasts with glob("uploads/slideShow/*")which will return the full path to the files you are looking at. If you use this globwith is_file, it should work fine.

+6

, , ... :

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

Source: https://habr.com/ru/post/1543839/


All Articles