neridaj Posted February 19, 2008 Share Posted February 19, 2008 Hello, I have a function that returns a boolean, which I am then using to provide a link or not to provide a link. Sounds simple enough, but I just don't see what I am doing wrong. The boolean function returns true and prints links to the page for each valid folder in a directory. However when I try to use that return in another function to provide a link based on "true" it never happens. should I use global variables instead? Here is the code: <?php function display_folders() { echo '<table border="1" cellpadding="5">'; $dir = 'members/' . $_SESSION['valid_user']; $files = scandir($dir); foreach($files as $value) { // check for folders if(valid_folder($value)) { echo '<tr><td><a href="photos.php?pa=' . $value . '"><img src="images/files.gif" width=75" height="75 />"<a/></td><td>' . $value . '</td></tr>'; return true; }else{ return false; } } echo '</table>'; } function valid_folder($folder) { // check if file is not an image or ./../ if (!ereg('^([a-zA-Z].*|[1-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F)))|[.|..]$', $folder)) return true; else return false; } function display_user_menu() { ?> <hr /> <a href="member.php">Home</a> | <?php // only offer the get photos option if valid folder is found if(display_folders()) echo '<a href="preview.php">Get Photos</a> | '; else echo "<font color='#cccccc'>No Photos</font> | "; ?> <a href="change_passwd_form.php">Change password</a> <a href="logout.php">Logout</a> <hr /> <?php } ?> Thanks for any help, Jason (edited by kenrbnsn to add tags) Quote Link to comment Share on other sites More sharing options...
duclet Posted February 19, 2008 Share Posted February 19, 2008 The first result of scandir is almost always "." followed by "..". According to your function declaration, that is returned false so it is doing what it is supposed to be doing. Also, couldn't you just use the function is_dir to see if a folder is valid or not? Quote Link to comment Share on other sites More sharing options...
neridaj Posted February 19, 2008 Author Share Posted February 19, 2008 The regex is: !ereg('^([a-zA-Z].*|[1-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F)))|[.|..]$', $folder) which filters out images and ./.. I figured out what was wrong with this anyways, but now I'm curious about the is_dir() you mentioned. When I use is_dir() the only directories returned are ./.. i.e., it doesn't include directories other than those. I would rather use this function than the regex, so I'm looking into it. Thanks a lot, Jason Quote Link to comment Share on other sites More sharing options...
chronister Posted February 19, 2008 Share Posted February 19, 2008 <?php $path = '/path/to/some/stuff'; if(is_dir($path)) { echo 'This is a directory'; } else { echo 'This is not a directory'; } ?> This is a very simple example, but is_dir or is_file are both really handy in instances like this. Nate Quote Link to comment Share on other sites More sharing options...
duclet Posted February 20, 2008 Share Posted February 20, 2008 Make the first line in your for loop an if statement. Check to see if the $value is '.' or '..'. If that is the case, skip the iteration. Otherwise, you can do the test. <?php foreach($files as $value) { if(in_array($value, array('.', '..'))) { continue; } // check for folders if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) { printf('<tr><td><a href="photos.php?pa=%s">'. '<img src="images/files.gif" width=75" height="75 />'. '<a/></td><td>%s</td></tr>', $value, $value); return true; } else { return false; } } ?> Quote Link to comment Share on other sites More sharing options...
neridaj Posted February 20, 2008 Author Share Posted February 20, 2008 Thanks for the insight into is_dir(), however, for some reason only the last $value is retained for each directory i.e., there are three valid directories but they all have the same link and the table cells containing the $value are not printed. Thanks again though, Jason Quote Link to comment Share on other sites More sharing options...
duclet Posted February 21, 2008 Share Posted February 21, 2008 That is because you are using your function for all the different folders. Your function is currently set up in a way that whenever it finds a valid folder, it returns true and terminates. The next time you call it, it will do the same thing but start from the beginning again so you are not seeing the next folders. If you want to check each folder individually, just use the is_dir function directly. Quote Link to comment Share on other sites More sharing options...
neridaj Posted February 21, 2008 Author Share Posted February 21, 2008 I should have mentioned that I divided this function up into two different functions because I actually needed to check if there were folders before I displayed the link to actually display the folders. So, I had modified your function by removing the return true/false and using it in the check function which should have eliminated the termination of the original function as it would now only print for each iteration. For some reason (operator error?) it is working perfectly now and I really want to thank you for the help. Thanks, Jason Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.