Forum Moderators: coopster

Message Too Old, No Replies

Drop Down Directory Content - PHP

PHP Drop Down

         

lander06

7:07 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



New to PHP and thought I take this challenge but I'm losing. I want to create a drop down box based upon directory listing. Once an item is selected, it will bring up another drop down box box with its contents. Example:

CapturedFiles---->Site---->IP----->File

Once I get to the file, I can click and download to local machine. Here is what I have so far:

<?php

$dir = "CapturedFiles";
if (isset($_GET['Site'])) { $Site = $_GET['Site']; } else { $Site = $_POST['Site']; }
if (isset($_GET['IP_Address'])) { $IP_Address = $_GET['IP_Address']; } else { $IP_Address = $_POST['IP_Address']; }

if (!(isset($Site)))
{
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<select name="Site" id="Site">
<?php
$output = shell_exec('ls ./'.$dir.'/');
$dirs = explode("\n", $output);
foreach ($dirs as $key => $value)
{
?> <option value="<?php echo $value?>"><?php echo $value?></option><?php
}
?>
</select>
</form>
<?php
}

if (isset($Site))
{
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<select name="Site" id="Site">
<?php
$output = shell_exec('ls ./'.$dir.'/');
$dirs = explode("\n", $output);
foreach ($dirs as $key => $value)
{
if ($value == $Site)
{
?> <option value="<?php echo $value?>" SELECTED><?php echo $value?></option><?php
}
else
{
?> <option value="<?php echo $value?>"><?php echo $value?></option><?php
}
}
?>
</select>
<select name="IP_Address" id="IP_Address">
<?php
$output = shell_exec('ls ./'.$dir.'/'.$Site.'/');
$dirs = explode("\n", $output);
foreach ($dirs as $key => $value)
{
?> <option value="<?php echo $value?>"><?php echo $value?></option><?php
}
?>
</select>
</form>
<?php
}

?>

I can't seem to get off the first level.

Matthew1980

7:31 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there lander06,

Welcome to the forum :)

Well just a quick scan of your code reveals 2 issues.

Firstly

Check the echoing out: <option value="<?php echo $value?>"><?php echo $value?>

They have no semi-colons on the end to indicate that they need echoing ;) try this:-

<option value="<?php echo $value;?>"><?php echo $value;?>?>

Go over the rest of the code to see what I mean.

Secondly
The use of $_SERVER['PHP_SELF']; don't use this if you can help it especially if you intend to go live with this code/site as there are vulnerabilities with it, google $_SERVER['PHP_SELF']; to see what I mean. Instead just leave the action attribute blank ie: <form method="post" action=""> this will then keep you compliant with the sgml parser (W3C schools)

Question though, how are you getting this:-

CapturedFiles---->Site---->IP----->File

Because you are checking for both $_POST & $_GET, $_POST is from a form element & $_GET is from an URL.

I'm not sure what you are doing with the shell_exec, so I'll assume as you are getting the desired output from the array that is returned.

If you are looking for dir/file recursion try ajax, as there are some pretty good "out of the box" things out there, and more than likely someone has done one in PHP somewhere :)

Hope that helps a little.

Cheers,
MRb

Readie

7:44 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gonna argue some petty points with you here Matt :P

The use of $_SERVER['PHP_SELF']; don't use this if you can help it especially if you intend to go live with this code/site as there are vulnerabilities with it, google $_SERVER['PHP_SELF']; to see what I mean. Instead just leave the action attribute blank ie: <form method="post" action=""> this will then keep you compliant with the sgml parser (W3C schools)

Agreed with the "Don't use PHP_SELF", but, disagree with others.

action=""
Is not, I believe, semantic. Instead, I suggest you do this:

action="<?php echo $_SERVER['REQUEST_URI']; ?>"

Matthew1980

7:54 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie,

I half wondered if you would pull me up on that when I was typing ;-p

As you commented a while back, you can leave the attribute blank, <form action="" method="post"> or <form method="post"> both will work if you are not fussy about being compliant, but if you are, then put action="" and leave blank as it defaults to the same page.

Granted you can use this method as you have highlighted to me, I guess its a matter of preference at the end of the day :)

Also, that was just pedantic! petty would be pulling me up on grammar too ;-)

Cheers,
MRb

lander06

8:08 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



Matt,
Thank you for the quick reply.

This script i'm testing in my home network with files I captured from a project. This list will grow..a lot! Here is an example:

CaptureFiles
Atlanta
192.168.1.1
profile.xml

I will change the echo suggestion.

Readie

9:33 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As you commented a while back, you can leave the attribute blank, <form action="" method="post">

I was wrong :) Or, atleast insofar as the XHTML doctype is concerned I was wrong. Ran it through the validator today and it told me off :( It does work though, but it isn't really much effort to call the $_SERVER['REQUEST_URI'].

petty would be pulling me up on grammar too ;-)

I can start if you'd like? :P You forgot to capitalise "petty" after an exclamation mark :D

Matthew1980

9:42 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah, Readie,

That's why I did it, ex-proof reader you see! ;-p (I get sloppy with grammar when tired though...)

I will have to check this now :( I have recently gone from transitional to strict and I haven't had an error yet (touch wood). So thank you for pointing that out to me.

Cheers,
MRb

rocknbil

6:34 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



New to PHP and thought I take this challenge but I'm losing. I want to create a drop down box based upon directory listing. Once an item is selected, it will bring up another drop down box box with its contents.


Welcome aboard lander06, this is pretty common and is not all that difficult. Think of three conditions:

If 1 and 2, submit search.

else if 1 and not 2, return to form populating the value of list 2 with what's selected in 1.

else output the form with just 1.

A simplified example (may contain errors, and has very little logic for cleansing input:)


if (
(isset($_POST['list1']) and is_numeric($_POST['list1']) and ($_POST['list1'] > 0)) and
(isset($_POST['list2']) and is_numeric($_POST['list2']) and ($_POST['list2'] > 0))
) { $output = do_search(); }
else if (
(isset($_POST['list1']) and is_numeric($_POST['list1']) and ($_POST['list1'] > 0)) and
(! isset($_POST['list2']) or ! is_numeric($_POST['list2']) or ! ($_POST['list2'] > 0))
) {
$sub_option=sub_option_list($_POST['list1']);
$output = output_form($sub_option);
}
else { $output = output_form(); }
header("content-type:text/html");
echo $output;
//
// -------------- Done with program, functions follow
//
// not complete, just showing the logic
//
function output_form($options=null) {
$form = '<form method="post" action="some-script.php">
<label for="list1">Main Cat:</label>
<!-- note the onchange event handler . . . -->
<select name="list1" id="list1" onchange="this.form.submit();">
';
// however you get your list
$query = "select id,title from category order by title";
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {
$form .= '<option value="' . $row[0] . '"';
// Or, selected="selected" for XHTML, if you're doing any extending.
if ($_POST['list1']==$row[0]) { $form .= ' selected'; }
$form .= '>' . $row[1] . '</option>';
}
$form .= '</select>';
if ($options) {
$form .= '<label for="list2">Sub Category:</label>' . $options;
}

else { $form .= '&lt;-- Select Main Category'; }
$form .= '<input type="submit" value="next"></form>';
return $form;
}
//
// Do the SAME THING here as above, but use the
// main category to: select * from subcats where category = $category
//
function sub_option_list($category) {
if (! is_numeric($category) or ! ($category>0)) {
die("Oops/ Somehow you are requesting sub cat, but no main category.");
}
// carry on, create select list as above . . .
return $select_list;
}
//
// calling it search, whatever your function is
//
function do_search() {
//
return $search_results;
}