Basic php, dropdown and functions question

Associate
Joined
4 Apr 2003
Posts
1,805
Location
Manchester
I am a complete novice to php, scripting and html. I am trying to dynamically generate a dropdown box with a list of folders and then I want to generate a list links to the files for the selected folders. I have been searching for the solution which I am sure is really obvious but I don't full understand the code structure

I have this to generate the dropdown box and populate with the folders in the "Docs\" directory

Code:
<!DOCTYPE html>
<html>
<body>

<select id="mySelect" >

<?php
    $dirs = glob("Docs\*", GLOB_ONLYDIR);
    foreach($dirs as $val)
    {
    echo '<option value="'.$val.'">'.basename($val)."</option>\n";
    }   
?>  

</select>
</body>
</html>

I also have this to generate links from a set folder

Code:
<!DOCTYPE html>
<html>
<body>


<?php
foreach (glob("Docs\PB02\*.pdf") as $pathtodocs)
    {
    $filename = basename($pathtodocs);
    echo "<a href=\"$pathtodocs\">$filename</a>";
    echo "<br>";
    } 
?>

</body>
</html>
What I don't understand is how to call the second set of code as a function based on the first set of code and have it display underneath the drop down box. I have tried with onchange but as I say, I must be missing something basic
 
Associate
Joined
4 Feb 2011
Posts
580
Location
Halifax
You'll probably want to use $_GET variables.

PHP:
<!DOCTYPE html>
<html>

<body>
    <form action="" method="GET">
        <select id="directories" name="directory" onchange="this.form.submit()">
            <option>Choose One</option>
            <?PHP
            $directories=glob("Docs\\*", GLOB_ONLYDIR);
            foreach($directories as $directory) {
                echo '<option value="' . basename($directory) . '">' . basename($directory) . "</option>"; 
            } 
            ?>
        </select>

        <?PHP
        if(isset($_GET["directory"])) {
            foreach(glob("Docs\\" . $_GET["directory"] . "\\*.pdf") as $file) {
                echo '<a href="' . $file . '">' . basename($file) . '</a>';
            }
        }
        ?>
    </form>
</body>

</html

Just a couple things, the backslash acts as an escape character in strings, therefore needs to be escaped with itself.
Sanitization. (In it's current state, someone could just have the directory as ?directory=../../ and it would go up two directories)
 
Last edited:
Associate
OP
Joined
4 Apr 2003
Posts
1,805
Location
Manchester
thanks. I have been struggling with $_GET and $_POST.

This isn't quite working. Is there a way I can get it to echo the output og the $_GET statement so I can see what is happening?
 
Associate
OP
Joined
4 Apr 2003
Posts
1,805
Location
Manchester
got it! it was going for docs\docs\foldername

Code:
<!DOCTYPE html>
<html>

<body>
    <form action="" method="GET">
        <select id="directories" name="directory" onchange="this.form.submit()">
		<option value="Select">Select Folder</option>;
            <?PHP
            $dirs=glob("Docs\\*", GLOB_ONLYDIR);
            foreach($dirs as $val) {
                echo '<option value="' . $val . '">' . basename($val) . "</option>"; 
            } 
            ?>
        </select>

		<p>
        <?PHP
        if(isset($_GET["directory"])) {
            foreach(glob($_GET["directory"] . "\\*.pdf") as $file) {
                echo '<a href="' . $file . '">' . basename($file) . '</a>';
				echo "<br>";
            }
        }
        ?>
		</p>
    </form>
</body>

</html>

Thankyou so much for your help
 
Associate
Joined
4 Feb 2011
Posts
580
Location
Halifax
Thanks.

So the drop down is working but I can't see the listing of the links generated from the folder?

What does your code look like? If you copied mine there may be a couple mistakes. :p

Edit:

I strongly suggest you don't use

PHP:
glob($_GET["directory"] . "\\*.pdf")

Since someone can just do ?directory=C: same goes with relative paths (../).

At least append it with a basepath like you had before (Docs)
 
Last edited:
Soldato
Joined
6 Feb 2004
Posts
20,674
Location
England
here you go...

Code:
<?php
$base = "Docs/";
$dirs = array_map("basename", glob($base . "*", GLOB_ONLYDIR));
$directory = isset($_GET['directory']) && in_array($_GET['directory'], $dirs) ? $_GET['directory'] : "";
?>
<!doctype html>
<html>
<body>
<form action="" method="GET">
<select id="directories" name="directory" onchange="this.form.submit()">
<option value="">Select folder...</option>
<?php
foreach($dirs as $val) {
	echo '<option ' . ($directory == $val ? 'selected="selected" ' : '') . 'value="' . $val . '">' . $val . "</option>\n"; 
}
?>
</select>
</form>
<?php
if(is_dir($base . $directory)) {
	foreach(glob($base . $directory . "/*.pdf") as $file) {
		echo '<a href="' . $file . '">' . basename($file) . '</a><br>' . "\n";
	}
}
?>
</body>
</html>

edit: i just changed the code on line 4 because you may have seen warnings depending on your php error display settings. i fixed that by using isset with $_GET['directory'].
 
Last edited:
Back
Top Bottom