alant Posted November 27, 2006 Share Posted November 27, 2006 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]<?phpif (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 More sharing options...
btherl Posted November 27, 2006 Share Posted November 27, 2006 Are the folders in the same location as your script?Also, file_exists() tells you if the folder exists, not whether there are files inside it. Link to comment https://forums.phpfreaks.com/topic/28590-file_exists-help/#findComment-130825 Share on other sites More sharing options...
genericnumber1 Posted November 27, 2006 Share Posted November 27, 2006 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.phpandreaddir() - http://www.php.net/manual/en/function.readdir.php[code=php:0]// Checks for files in $directory, returns true if files existfunction 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.