Jump to content

file_exists Help!


alant

Recommended Posts

Hi,
I'm new to PHP and coding in general and I'm having a problem with the file_exists command and was hoping someone could tell me what I was doing wrong.
I have a group of folders that I want to check if there are any files inside of them or not. So I wrote this bit of code:
[code]<?php
if (file_exists("./Folder1")) { echo "A file exists in Folder 1!";}
else echo "No file exists in Folder1";
if (file_exists("./Folder2")) { echo "<br>A file exists in Folder 2!";}
else echo "<br>No file exists in Folder2";
if (file_exists("./Folder3")) { echo "<br>A file exists in Folder 3!";}
else echo "<br>No file exists in Folder3";
?>[/code]

I manually made each folder and they are inside the same working dir as the php file. I also allowed everyone read/execute permissions on the folders. However when I run it file_exists always returns a FALSE attrib even when there are files in the folders. I am running IIS6 with PHP v5.2(ISAPI). However, I was having this same problem when I was previously running v5.1 (CGI). Any help someone could offer would be appreciated.
Thanks,
Link to comment
Share on other sites

file_exists() just tells you if a file or directory exists, it doesn't check to see if files exist inside the directory.

[s]you'd want to use[/s][i] (edit: see 2nd function for what you REALLY want to use)[/i]
opendir() - http://www.php.net/manual/en/function.opendir.php
and
readdir() - http://www.php.net/manual/en/function.readdir.php

[code=php:0]
// Checks for files in $directory, returns true if files exist
function check_for_files($directory){
  $handle = opendir($directory);
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
        closedir($handle);
        return true;
      }
  }
  closedir($handle);
  return false;
}
[/code]

This also [s]MIGHT[/s] [i]does[/i] work.. [s]but hasn't been tested...[/s]

[code=php:0]
function check_for_files($directory){
  $num_files = count(glob($directory . "*"));
  if($num_files > 0) {
      return true;
  } else {
      return false;
  }
}
[/code]

[i][b]Edit:[/b] Tested the 2nd function, works fine... probably is much faster than the first as well, favor the latter :D[/i]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.