Jump to content

Why is_file isn't seeing files?


Go to solution Solved by phppup,

Recommended Posts

I thought this made sense, but it wouldn't work until I removed lines that I thought would provide extra validation:

$dir = 'rootFolder';
$directories = scandir($dir);
foreach($directories as $directory){
    if($directory=='.' or $directory=='..' ){
        echo 'dot';
    }else{
    if(is_dir($directory))
echo '<strong>'.$directory .'</strong> <br>';
    $filePath = scandir($directory);
foreach($filePath as $subDir){
if($subDir=='.' or $subDir=='..' ){
        echo 'dot2 <br>';
    }else {
   // if (is_file($subDir)) {
//Only provided  PHP files but NO IMAGES
        echo $subDir . "<br>";
    }  }  // }

            }
    }
} 

I'm just feeling down one level of a director to see what I've get. It seemed like a simple exercise until I tried to list the files.

Obviously, of it's not a directory it MUST be a file, but why did quantifying it as a file not recognize my files as a file??

Link to comment
https://forums.phpfreaks.com/topic/329643-why-is_file-isnt-seeing-files/
Share on other sites

1 hour ago, phppup said:
if(is_dir($directory))

the above line is missing any {  }, so the only line of code that gets executed for an is_dir() is the - echo '<strong>'.$directory .'</strong> <br>'; all the rest of the lines get executed regardless of what $directory is.

i recommend that you always format your code so that you can see when it is actually doing.

  • Great Answer 1
  • Solution

@mac_gyver yes, I had discovered that, but it wasn't the actual fix.

The real issue was that my test for is_file was flawed; it didn't drill down to actually test the specified file.

if (is_file($subDir)) {

//needed to become  
if (is_file($directory.'/'.$subDir)) {

After both adjustments, everything is running better than expected!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.