PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
GirishR
Forum Newbie
Posts: 11 Joined: Wed May 19, 2004 6:48 am
Contact:
Post
by GirishR » Wed May 19, 2004 6:48 am
Hello Guys,
Please try this piece of code
Code: Select all
<?php
if (!$folder)
{
$folder = ".";
}
chdir($folder);
print "<BR><BR>Current Folder -> $folder<BR>";
//print gettype($folder);
getDirList($folder);
function getDirList($dirName)
{
$d = dir($dirName);
print $dirName."<BR>";
while($entry == $d->read())
{
if ($entry != "." && $entry != "..")
{
if (!is_file($entry))
{
//print $entry;
print "<a href=directory_browser1.php3?folder=".htmlentities(urlencode($dirName))."/".htmlentities(urlencode($entry))."><font size=2 color=blue face=Tahoma>$entry</font></a><BR>";
//getDirList($dirName."/".$entry);
}
else
{
//echo $dirName."/".$entry."\n";
}
}
}
//$d->close();
}
?>
This gives me werid error. Logicaly, I think every thing is correct. Can any one on of you fix it. please.
Regards
Girish R
?>[/php_man]
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed May 19, 2004 6:59 am
What's the error you're getting?
Btw: I'm using this function which works quite nice:
Code: Select all
<?php
function get_directory_contents($path){
$mydirectory = opendir($path);
while($entryname=readdir($mydirectory)){
switch (true){
case ($entryname==".") : break;
case ($entryname=="..") : break;
case (is_dir($path."/".$entryname)) : $return["dir"][]=$entryname; break;
default : $return["file"][]=$entryname;
}
}
closedir($mydirectory);
return $return;
}
echo "<h3>directory contents</h3><pre>";
print_r(get_directory_contents("./"));
echo"</pre>";
?>
GirishR
Forum Newbie
Posts: 11 Joined: Wed May 19, 2004 6:48 am
Contact:
Post
by GirishR » Wed May 19, 2004 7:15 am
Ok patrikG, thats for script is for displaying th contents of a folder. But what I have coded is for browsing the directory by clicking each of the directory. It works fine for the first time but when clicked it gives a fatal error.
Thanks
Girish R
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed May 19, 2004 7:47 am
What fatal error are you getting?
Also, change
to
GirishR
Forum Newbie
Posts: 11 Joined: Wed May 19, 2004 6:48 am
Contact:
Post
by GirishR » Wed May 19, 2004 7:55 am
Here is the error
Warning: OpenDir: Invalid argument (errno 22) in c:\apache\htdocs\girishr\directory_browser1.php on line 12
./FCKeditor_1.6
Fatal error: Call to a member function on a non-object in c:\apache\htdocs\girishr\directory_browser1.php on line 14
Yes i did change the while($entry = $d->read()) to while($entry == $d->read())
Thanks
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed May 19, 2004 8:06 am
I've changed $folder to $_GET["folder"] (see
http://www.devnetwork.net/forums/viewtopic.php?t=511 ) and added a if (is_dir($dir)) to your function.
Code: Select all
<?php
if (!$_GET["folder"])
{
$_GET["folder"] = ".";
}
print "<BR><BR>Current Folder -> $folder<BR>";
getDirList($_GET["folder"]);
function getDirList($dirName)
{
if (is_dir($dirName)){
$d = dir($dirName);
print $dirName."<BR>";
while($entry = $d->read())
{
if ($entry != "." && $entry != "..")
{
if (!is_file($entry))
{
//print $entry;
print "<a href=temp1.php?folder=".htmlentities(urlencode($dirName))."/".htmlentities(urlencode($entry))."><font size=2 color=blue face=Tahoma>$entry</font></a><BR>";
//getDirList($dirName."/".$entry);
}
else
{
//echo $dirName."/".$entry."\n";
}
}
}
//$d->close();
}
}
?>
GirishR
Forum Newbie
Posts: 11 Joined: Wed May 19, 2004 6:48 am
Contact:
Post
by GirishR » Wed May 19, 2004 8:12 am
i got a 500 Internal Server Error.
GirishR
Forum Newbie
Posts: 11 Joined: Wed May 19, 2004 6:48 am
Contact:
Post
by GirishR » Wed May 19, 2004 8:15 am
Sorry, my mistake did not change the file name. BTW what was the problem. A global variable?
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed May 19, 2004 8:16 am
That's a server-issue - restart your server.
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed May 19, 2004 8:17 am
The problem was that you tried to retrieve directory-contents of files and thus got error-messages back. The [php_man]is_dir[/php_man] if-clause I've added to your function prevents that.
GirishR
Forum Newbie
Posts: 11 Joined: Wed May 19, 2004 6:48 am
Contact:
Post
by GirishR » Wed May 19, 2004 8:22 am
Thank you friend. That was very help full. BTW I am doing a search engine in php. So I wanted to add this feature fo users to add directories by selecting them rather that typing them. You can see the search engine script in
http://www.hotscripts.com/Detailed/33759.html .
Thanks
Girish R
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed May 19, 2004 8:26 am