LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2007, 04:25 AM   #1
compsci13
LQ Newbie
 
Registered: Jun 2007
Posts: 8

Rep: Reputation: 0
Weird Directory/PHP problem


Hello all,

Objective: Is to go through a directory
PHP Code:
$dir '/home/public_html'
, Retrieve names of existing directories only and put them in an array and sort them. The following php script works for the main directory - my main domain (http://www.flatstorentlive.co.uk) because the script is shown at the bottom and returns the correct values "Fulham" and "Notting Hill". But when i use the same script for the subdomains which each have their own index page i get nothing! Even though the variable $dir is set and it cant change! The other pages are (http://fulham.flatstorentlive.co.uk) and (http://nottinghill.flatstorentlive.co.uk)

Here is the script:

PHP Code:
<?php
$dir 
'/home/public_html';
$dh  opendir($dir);
while (
false !== ($filename readdir($dh))) {
if(
is_dir($filename)){
    
$files[] = $filename;
    }
}
sort($files);
for(
$i=0$i<sizeof($files); $i++){
if((
$files[$i]!="cgi-bin") and ($files[$i]!=".") and ($files[$i]!="..") and ($files[$i]!="suspended.page")){
?>
<a style="font-size:12px; font-family:Arial, Helvetica, sans-serif" href="http://<?php echo $files[$i?>.flatstorentlive.co.uk">
<?php echo strtoupper(str_replace('-',' ',$files[$i])); ?></a>&nbsp;
<?php } } closedir($dh);?>
Thanks for any help!
 
Old 06-27-2007, 04:28 AM   #2
compsci13
LQ Newbie
 
Registered: Jun 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Just to add to this. The index pages are actually only differen because of the meata tags. This script above is used like this in the index pages:
PHP Code:
<?php require("/home/public_html/footer.php"?>
 
Old 06-27-2007, 04:41 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

"readdir" returns the name of what it finds inside the directory without the directory name first. So your script only works if current directory is /home/public_html. To fix:

PHP Code:
while (false !== ($filename readdir($dh))) {
  
$filename $dir.'/'.$filename;
  if(
is_dir($filename)){
    
$files[] = $filename;
  }

Edit:
Another option is to just put a "chdir($dir)" before the opendir. I see that later in your script, you assume the filenames don't have a directory prefix.

Last edited by Guttorm; 06-27-2007 at 04:44 AM.
 
Old 06-27-2007, 04:49 AM   #4
compsci13
LQ Newbie
 
Registered: Jun 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks Guttorm! It sort of works but another problem has arisen! The changes i made that you suggested i came up with these results:

/HOME/FLATSTOR/PUBLIC_HTML/. /HOME/FLATSTOR/PUBLIC_HTML/.. /HOME/FLATSTOR/PUBLIC_HTML/CGI BIN /HOME/FLATSTOR/PUBLIC_HTML/FULHAM /HOME/FLATSTOR/PUBLIC_HTML/NOTTING HILL /HOME/FLATSTOR/PUBLIC_HTML/SUSPENDED.PAGE

WHICH IS EXCELLENT, but...

I am new to php, so how do i strip away the path, to be left with just the the final directories as names like this:

Fulham
Notting Hill
CGI Bin....etc

Thanks for more help!
 
Old 06-27-2007, 05:03 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi again

To only get the filename part:
$filename = BaseName($filename);
 
Old 06-27-2007, 05:21 AM   #6
compsci13
LQ Newbie
 
Registered: Jun 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Hello Guttorm, sorry to bother you again but it doesn't seem to work!!! Not the basename but the script! It was working a minute a go with your suggestion but now it doesn't here is all the changes i made:
PHP Code:
<?php
$dir 
'/home/flatstor/public_html';
$dh  opendir($dir);
while (
false !== ($filename readdir($dh))) {
$filename $dir.'/'.$filename;
$filename basename($filename);
if(
is_dir($filename)){
    
$files[] = $filename;
    }
}
sort($files);
for(
$i=0$i<sizeof($files); $i++){
if((
$files[$i]!="cgi-bin") and ($files[$i]!=".") and ($files[$i]!="..") and ($files[$i]!="suspended.page")){
?>
<a style="font-size:12px; font-family:Arial, Helvetica, sans-serif" href="http://<?php echo $files[$i?>.flatstorentlive.co.uk">
<?php echo strtoupper(str_replace('-',' ',$files[$i])); ?></a>&nbsp;
<?php } } closedir($dh);?>
I have changed the use though i call it like this now:

PHP Code:
<?php require_once("/home/flatstor/public_html/subdomain.php"?>
As before it only works for the main domain but not the subdomain.

Thanks for anymore help!
 
Old 06-27-2007, 05:23 AM   #7
compsci13
LQ Newbie
 
Registered: Jun 2007
Posts: 8

Original Poster
Rep: Reputation: 0
No wait! I put the code you gave me in the wrong place, i put after the if statement now and it works!

THANK YOU VERY MUCH Guttorm!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
weird PHP problem: Call to undefined function mysql_connect() Shioni Linux - Server 4 12-22-2006 05:26 AM
weird: directory permissions change inadvertently stabu Slackware 3 12-03-2006 12:20 PM
weird Php problems 10.2 dbc001 Slackware 8 01-28-2006 01:52 PM
How to copy one directory to another directory thru PHP? green_njk Programming 1 01-06-2006 06:55 AM
weird PHP error atom Programming 5 09-26-2004 08:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration