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
https://forums.phpfreaks.com/topic/28590-file_exists-help/
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
https://forums.phpfreaks.com/topic/28590-file_exists-help/#findComment-130828
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.