geoffjb Posted November 4, 2006 Share Posted November 4, 2006 I'm trying to list all the folders and files in the current folder, except for those designated to be ignored. I initally thought I should put the file names that should be ignored into an array, as this would be easy for my colleague (who does not know PHP but has FTP access) to add file names himself.Here is the code I started with. You'll notice that the file names are included directly in the if statement. Although I could leave it like this, I'd rather pull these names out to define them right away.[code]<?php$path = getcwd();$dh = opendir($path);print "<h2>Directories</h2>\n\n<ul>\n"; while (($dir = readdir($dh)) !== false) { if ($dir != "." && $dir != ".." && $dir != ".ftpquota"){ $file = @fopen("$path/$dir", "r"); if (!$file){ print "<li><a href='$dir'>$dir</a></li>\n"; } } }print "</ul>\n\n"; closedir($dh);$path = getcwd();$dh = opendir($path);print "<h2>Files</h2>\n\n<ul>\n"; while (($dir = readdir($dh)) !== false) { if ($dir != "index.php" && $dir != "test.php" && $dir != "test2.php"){ $file = @fopen("$path/$dir", "r"); if ($file){ print "<li><a href='$dir'>$dir</a></li>\n"; } } }print "</ul>\n\n"; closedir($dh);?>[/code]As you can see, the two while loops are nearly identical.Below is the code I have written to date. Comments are intertwined. All comments directed toward this forum are between /* and */ (as opposed to // comments).[code]<?php// Add files or folders below that should be excluded from the main page.// // Ensure that every file name is wrapped in single quotes (e.g. 'filename.pdf'),// and that all except the final file name have a comma after them.$excluded = array ( '.', '..', 'index.php', '.ftpquota');for ($excluded[$i=0]; $excluded[$i]; $excluded[$i++]) { $hidename[$i] = "$dir != \"$excluded[$i]\" && ";}/* I have been back and forth on what characters above need to be escaped and which can be left as they are.Basically, I'm trying to include each folder and file name in the if statement, so that they do not get printed.The result should look something like the if-conditions above (that is, if ($dir != "this" && $dir != "that" && ...). */for ($hidename[$i=0]; $hidename[$i]; $hidename[$i++]) { $hidden += $hidename[$i];}/* This takes each of the $hidename[] values, and groups it into one big statement. */print_r($hidden, true);substr_replace($hidden, '', -3);print_r($hidden, true);/* This was to get rid of the last && signs that would inevitably end up in the $hidden variable.I printed before and after to see if I was removing enough characters, but I could never get thevalue of $hidden to print. */$path = getcwd();$dh = opendir($path);print "<h2>Directories</h2>\n\n<ul>\n"; while (($dir = readdir($dh)) !== false) { if ($hidden){/* I think this line (above) is the troublemaker. As it stands, it will of course return true, because the variable$hidden exists. I've tried putting return, for loops, while loops, etc. in the conditional part of theif-statement here, and I always get an error (in the form T_ERROR, where ERROR is the type of functionI tried [such as T_WHILE]). */ $file = @fopen("$path/$dir", "r"); if (!$file){ print "<li><a href='$dir'>$dir</a></li>\n"; } } }print "</ul>\n\n"; closedir($dh);$path = getcwd();$dh = opendir($path);print "<h2>Files</h2>\n\n<ul>\n"; while (($dir = readdir($dh)) !== false) { if ($hidden){ $file = @fopen("$path/$dir", "r"); if ($file){ print "<li><a href='$dir'>$dir</a></li>\n"; } } }print "</ul>\n\n"; closedir($dh);?>[/code]All help and criticism is readily welcomed. Quote Link to comment Share on other sites More sharing options...
geoffjb Posted November 4, 2006 Author Share Posted November 4, 2006 I just added a new directory, and that code put it under the files. Perhaps I'll have to rethink the code I use.In any event, the question is still valid, as I will need to know how to incorporate each value of an array into an if-condition.Thanks. Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 4, 2006 Share Posted November 4, 2006 Your code is rather complicated to follow and I'm not sure what's going on. But array_search will tell you if one particular value is part of an array.So given the "excluded" variable you've shown above, [b]array_search('index.php', $excluded);[/b] would return 2 Quote Link to comment Share on other sites More sharing options...
geoffjb Posted November 4, 2006 Author Share Posted November 4, 2006 Yeah, the code is rather difficult to follow. It was some of the code offered on php.net to return all the files in the current working directory. I'll be re-thinking that portion of the code. However, my underlying question still stands: How would I incorporate an array into an if statement?For example:[code]<?php$list = array ( 'one', 'two', 'three');if ($test != "one" && $test != "two" && $test != "three") { // do something}?>[/code]Rather than writing everything out in the if conditional, how could I return each value of the $list array? I tried foreach, for, and while in the parenthesis, but they returned errors.Thanks. Quote Link to comment Share on other sites More sharing options...
joshi_v Posted November 4, 2006 Share Posted November 4, 2006 Can't you use in_array function for your problem?it will be like this in your situation.[code]<?php$list = array ( 'one', 'two', 'three');if (!in_array($test,$list)//It will check whether $test in $list or not.{ // do something}?>[/code]Hope this helps you! Quote Link to comment Share on other sites More sharing options...
geoffjb Posted November 4, 2006 Author Share Posted November 4, 2006 That is exactly the function I need.Thanks a lot. 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.