Directories Help

kesne

New Member
Messages
47
Reaction score
0
Points
0
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.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Thanks to someone on PHP.net's opendir() function page, I found this

PHP:
<?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:

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
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
 

kesne

New Member
Messages
47
Reaction score
0
Points
0
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:

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
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:

kesne

New Member
Messages
47
Reaction score
0
Points
0
Thanks.

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

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
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
 
Top