Showing Directory Path and Suggestions

Following code shows directory structure for the current directory. As user types in path in text box, directory path is shown for that path. e.g. if there is a directory abc in root folder and user starts typing a then he will be shown directories and files starting with a in root folder.
In this code, if a matching directory is found, then it is shown in Green otherwise it is shown in Red.

This script can be used for drop down suggestions as user types in textbox.

Use these 3 files.

[php]index.php

dir.php

dirpath.js[/php]

Code for these files are:

index.php

[php]<html>
<head>
<title>Directory Path Structure</title>
<script src="dirpath.js"></script>
</head>
<body>

<?php
echo "
<form name=’test’>
<input type=’text’ name=’usrdirpath’ onkeyup="showdirpath(this.value, ‘displaydiv1′,’dir.php’)">
<br>
<input type=’submit’ value=’Test’ name=’testsub’>
</form>
";

echo "<div id=’displaydiv1′></div>";
?>

</body>
</html>[/php]

dir.php

[php]<script src="dirpath.js"></script>
<?php

if(isset($_REQUEST[‘usrdirpath’]))
{
$usrdirpath=$_REQUEST[‘usrdirpath’];

echo "<b>User Directory Path:</b> $usrdirpath<br>";

$last_dirsymbol_usr=’/’;
$last_dirsymbol=’\’;

$usrdirpath=str_replace($last_dirsymbol_usr, $last_dirsymbol, $usrdirpath);

echo "<b>User Converted Directory Path:</b>".$usrdirpath."<br>";

$lst_dirsymbol_pos=strrpos($usrdirpath, $last_dirsymbol);
echo "<b>Last Occurrence Position:</b> $lst_dirsymbol_pos<br>";

$usrdirpath_sub1=substr($usrdirpath, 0, $lst_dirsymbol_pos);
if($lst_dirsymbol_pos === false)
{
$usrdirpath_sub2=substr($usrdirpath, $lst_dirsymbol_pos);
}
else
{
$usrdirpath_sub2=substr($usrdirpath, $lst_dirsymbol_pos+1);
}

echo "<b>Directory Now:</b> $usrdirpath_sub1<br>
<b>After Directory:</b> $usrdirpath_sub2<br>";

$dirnam=dirname(__FILE__);
$dirnam_slash=$dirnam."\"; //No Trailing Slash//
$dirpath=$dirnam_slash.$usrdirpath_sub1;

echo "<br><br>";
echo "<b>Root Directory:</b> $dirnam_slash<br>";
echo "<b>Current Directory Path:</b> $dirpath<br>";
$directorynames = glob($dirpath.’/*’);

foreach ($directorynames as $filename)
{
$dir_lst_dirsymbol_pos=strrpos($filename, $last_dirsymbol_usr);
$dirpath_sub2=substr($filename, $dir_lst_dirsymbol_pos+1);

$strposchk=strpos($dirpath_sub2, $usrdirpath_sub2);

if($strposchk === false || $strposchk!="0")
{
echo "<font color=’red’>$dirpath_sub2</font><br>";
}
else
{
echo "<font color=’green’>$dirpath_sub2</font><br>";
}

}

}
else
{
echo "Enter Directory Path<br>";
}

?>

[/php]

dirpath.js

[php]// JavaScript Document

var xmlHttp;
function GetXmlHttpObject()
{
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

function showdirpath(usrdirpath,divId,PageUrl)
{
var msg = usrdirpath+divId+PageUrl;
//alert(msg);
//document.getElementById(divId).style.display="block";
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url=PageUrl;
Page=PageUrl+"?usrdirpath="+usrdirpath;
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4)

{
document.getElementById(divId).innerHTML=xmlHttp.responseText;
}

}
xmlHttp.open("GET",Page,true);
xmlHttp.send(null);

}

[/php]

Leave a Comment