+ Reply to Thread
Results 1 to 7 of 7

Thread: Directories Help

  1. #1
    kesne is offline x10Hosting Member kesne is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Tigard, Oregon
    Posts
    47

    Directories Help

    Hey there.

    I am making a PHP application and I need to know how to do the following.
    • Go into each sub-directory and find a config.php file
    • Read a setting off the file
    • Echo that setting in a dropdown list (the different directorys' settings fill up the dropdown.)
    Thanks in advance.

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Directories Help

    Thanks to someone on PHP.net's opendir() function page, I found this

    PHP Code:
    <?php
    //define the path as relative
    $path "path to directory";
    $webpath ="the url you want to place before your filename/";
    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");

    echo 
    "Directory Listing of $path<br/>";

    list_dir($dir_handle,$path);

    function 
    list_dir($dir_handle,$path)
    {
        
    // print_r ($dir_handle);
        
    echo "<ol>";
        
    //running the while loop
        
    while (false !== ($file readdir($dir_handle))) {
            
    $dir =$path.'/'.$file;
            if(
    is_dir($dir) && $file != '.' && $file !='..' )
            {
                
    $handle = @opendir($dir) or die("undable to open file $file");
                echo 
    "<li><a href='$webpath.$file'>$file</a></li>";
                
    list_dir($handle$dir);
            }elseif(
    $file != '.' && $file !='..')
            {
                   echo 
    "<li><a href='$webpath.$file'>$file</a></li>";
            }
        }
       
        echo 
    "</ol>";

        
    //closing the directory
        
    closedir($dir_handle);
       
    }
    ?>
    with a little work, you should be able to easily modify this to your needs ;)

    PS. using this method, check that $file == 'config.php', then do a $output = file_get_contents() to output the content and then check the file using either preg_match() or stristr() to check the setting you need.
    Last edited by xPlozion; 02-16-2009 at 08:32 PM.

  3. #3
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: Directories Help

    aww man, xPlozion you beat me to it. I was surfing the web someplace else and came back to find you already posted it

    you will be using scandir and is_dir to select each directory
    is_file to confirm a config.php file is present

    that only takes care of bullet point 1
    i'm busy right now so i will finish it up later

  4. #4
    kesne is offline x10Hosting Member kesne is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Tigard, Oregon
    Posts
    47

    Re: Directories Help

    Thanks guys, I'm still getting it perfected but it is working so far.
    Edit:
    I just ran into an issue.

    if ($file=='config.php'){
    $opened = file_get_contents($file);
    $stored = stristr($opened, '$name = ');
    echo"$stored";
    }

    What am I doing wrong? I'm trying to get the content after $name and it isnt echoing anything.
    Last edited by kesne; 02-16-2009 at 09:16 PM. Reason: Automerged Doublepost

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Directories Help

    I would make sure that your script is working as you expect it to in steps, I like to use var_dump() to see what is happening at that point.

    ie
    Code:
    var_dump($file);
    #next part of code is commented out so we can focus on what is happening before this part of the code
    /*if ($file=='confi.php'){
    ...
    echo"$stored";
    }
    */
    Make sure:

    1) $file is a file name without the path, it is not null, and it is at some point 'config.php' maybe you accidentally got the directory handle pointed to the wrong directory or you are getting 'public_html/config.php' which will never equal 'config.php'

    2) $opened is not null

    3) '$name = ' make sure that there is actually a space before and after the '=' for EVERY instance. also note that stristr will return everything after the search term. do you want 500 lines of text, or just everything up to a line break or semicolon?

    4) $stored is not null (it sounds dumb, but in your file does it say '$name = ' and then end of file? in this case, your script is working!)

    Best of luck!
    Last edited by garrettroyce; 02-16-2009 at 09:29 PM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  6. #6
    kesne is offline x10Hosting Member kesne is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Tigard, Oregon
    Posts
    47

    Re: Directories Help

    Thanks.

    I think a better approach would just to read the first line, thats it. How would I go about that?

  7. #7
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Directories Help

    Quote Originally Posted by kesne View Post
    Thanks.

    I think a better approach would just to read the first line, thats it. How would I go about that?
    fgets($handle,[$lenghth]) will read up to the optional length or newline or EOF, whichever is first. You now need a file handle instead of file name, so you will have to use fopen($file,'r') for read-only access.

    http://www.php.net/manual/en/function.fopen.php
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Similar Threads

  1. what are all these directories?
    By wbar8417 in forum Free Hosting
    Replies: 2
    Last Post: 06-13-2008, 11:23 AM
  2. Seo Directories
    By networker in forum Advertising
    Replies: 3
    Last Post: 04-02-2008, 09:32 AM
  3. Password Protect Directories Thread
    By swanniebroo in forum Programming Help
    Replies: 3
    Last Post: 02-07-2008, 06:57 PM
  4. Reset to original directories and files?
    By Jambu in forum Free Hosting
    Replies: 3
    Last Post: 12-10-2007, 02:55 AM
  5. Directories can help your page rank
    By Nuno_Aba in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 01-26-2006, 09:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers