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 $file
s variable:
Array
(
[0] => .
[1] => ..
[2] => 6696_930.jpg
[3] => 9_8912141076_L600.jpg
[4] => untitled file.txt
)
result for $sfiles
:
Array
(
)
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.
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!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With