 |
|
|
07-15-2006, 08:06 PM
|
Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
I just got a new website host on June 19th, and I'm already hosting 4 sites for some friends (via subdomains). I've made a section that will show the hosted sites. Though I want it to self-manage itself, meaning automatically updating with a PHP method. I have an idea for it, and I'm trying to implement it, but my method consists of a long if statement with a lot of AND's (&&). My idea is to make the method return a list of folders from the root directory except the ones that apply to my main site (such folders as _private, css, images, forums, cgi-bin). Right now, the if tells the method to only display if the listing is not any of those folders.
This is not what I want. All of those AND's in the if statement slows things down, and what's worse is that the function I'm using (read()) displays files too.
Might anyone have a method to list folders and only folders - even better would be subdomains if it's possible.
Thanks!
//KiroS
|
|
|
|
07-15-2006, 11:33 PM
|
Re: Folder Listing Method
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
I took your idea and tried to simplify it. Let me know if it helps (I have not tested it).
PHP Code:
<?php // Exclude list of directories not to show $exclude_array = Array ( '_private', 'css', 'images', 'forums', 'cgi_bin' ); $base_dir = '/home/public_html/'; $display = Array(); $d = dir($base_dir); while ($f = $d->read()) { if (!in_array($f, $exclude_array) && is_dir($f)) $display[] = $f; } // All unmarked directories are now in an array $display ?>
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-16-2006, 04:16 AM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
Thank you, I appreciate the help, though unfortunately, it's not displaying anything. I've tried changing the base directory and even using it in a literal/concrete way instead of with the variable. No dice. I'm assuming a bug is in the if statement, but it might be in the while... I don't use while's that much :P
Though like I said before, not even a single directory displays. 
|
|
|
|
07-16-2006, 07:00 PM
|
Re: Folder Listing Method
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
The sample above won't display anything, it just puts the results in an array. Try placing print_r($display); at the end to see the array elements.
* Also, you might need to add to the exclude array two more elements "." and ".." to exclude both current directory and parent directory. I finally tested, and it is working.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-16-2006, 11:26 PM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
(accidental early post)
Last edited by Kiros72; 07-16-2006 at 11:30 PM..
|
|
|
|
07-16-2006, 11:26 PM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
(accidental early post)
Last edited by Kiros72; 07-16-2006 at 11:29 PM..
|
|
|
|
07-16-2006, 11:29 PM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
Ok, thank you, I didn't realize that :P
Though are there any functions to return if there is more to the array and if so, move to the next item in the array. I'm thinking about using a for loop, but I'm unfamiliar with arrays in PHP. Are they like C++, where say you would be able to use a for loop as such:
Code:
for ($i = 0; $i < $display.length; $i++)
{
//some code
print_r($display[$i]);
/some more code
}
Sorry, I'm not really sure about the variables with the $'s either 
|
|
|
|
07-16-2006, 11:35 PM
|
Re: Folder Listing Method
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
A simple way to echo links would be like:
(appended to the sample script I show above)
PHP Code:
foreach($display as $value) { echo '<a href="http://www.domain.com/' . $value . '">' . $value . '</a><br />'; }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-17-2006, 07:07 PM
|
Re: Folder Listing Method
|
Posts: 111
Name: Jonathan
Location: Arizona, USA
|
I've used this one before: http://evoluted.net/community/code/directorylisting.php
Works pretty good for listing folders (haven't used it to do subdomains). They have a demo on their site.
__________________
Please login or register to view this content. Registration is FREE - Computer Repair over the internet!
|
|
|
|
07-17-2006, 10:14 PM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
Hmmm... This is odd... I have tried your code again, mgraphic, and I'm having a problem.
This code:
PHP Code:
<?php // Exclude list of directories not to show $exclude_array = Array ( '_private', 'css', 'images', 'forums', 'cgi_bin' ); $base_dir = '/home/kirosite/kirosite.com/html/'; $display = Array(); $d = dir("/home/kirosite/kirosite.com/html/"); while ($f = $d->read()) { if (!in_array($f, $exclude_array) && is_dir($f)) $display[] = $f; } // All unmarked directories are now in an array $display ?>
And this code:
PHP Code:
foreach($display as $value) { // Slightly changed anchor code echo '<a href="http://' . $value . '.mysite.com/">' . $value . ''s Site!</a><br />'; }
Is not working together :? I'm using PHP 4.4 by the way.
Does it work for you?
Last edited by Kiros72; 07-17-2006 at 10:16 PM..
|
|
|
|
07-18-2006, 12:30 PM
|
Re: Folder Listing Method
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
What is it outputting? Can you offer the url of where the script is so I can see the parsed output?
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-19-2006, 07:22 AM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
I have the first, very inefficient and not complete method at the top. After that, I have "-- Another Test --" to show the break and evidently (from looking at the source) that code causes an error because not even the ending body and html tags show up.
The website is: kiros (deletethis)ite.com/ (deletethis)first_try_homepage/ (deletethis)hostlist.php
I'm sorry about that somewhat messed up link, but I don't want any bots going to the site because it's still in it's very, very early stages and I haven't had any time to really work on it...
Edit:
Hmm... I changed the location to the literal html directory... Now it's working a little bit... I'll keep trying to see if I can get it to work.
Last edited by Kiros72; 07-19-2006 at 07:36 AM..
|
|
|
|
07-19-2006, 08:21 AM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
Small bump... I added a degging section and the PHP body code. Take a look, please... Sorry for the trouble though...
|
|
|
|
07-22-2006, 02:50 AM
|
Re: Folder Listing Method
|
Posts: 18
Name: Kiros
Location: Albany, LA - USA
|
Good news! I've figured out the problem. I do not have any idea why, but it's not accepting the directory I put in, so I'm assuming that it's defaulting to the path of the file. This is not a problem, as the path for the file will move to the root once I complete a basic part of the site.
Thank you a lot mgraphic!
I'm sorry for all the trouble I gave you!
Though there's just one more thing I need to touch on...
Do I have to use the close function after the main code? And if it's not imperative, should I still for security/stability reasons or does it not matter at all? O_o
Last edited by Kiros72; 07-22-2006 at 02:57 AM..
|
|
|
|
07-22-2006, 02:05 PM
|
Re: Folder Listing Method
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Congrads!
Do you mean using the directory closing function? I'm not an expert on this, but I think the benefit of this is to close it to allow for another instance to take place from another request process. I think (maybe) that once a dir is declared open, it is locked.
Please let me know if I'm off base on this...
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
|
« Reply to Folder Listing Method
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|