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
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 1:14 pm
Hi I have problem with listing dir... everything is ok in dir where script file is stored but one lvl up or down everyting sux. For example script show that file.txt is dir... error highlighting on line 2 ... How to fix it what to improve ?
index.php
Code: Select all
<?php
if($katalog=='')
$katalog='path/to/files';
if ($handle = opendir("$katalog")) {
while (false !== ($file = readdir($handle))) {
if(is_dir($file))
{
echo "<a href=index.php?show=$file>$file</a> - directory<br>";
}
else
{
echo "<a href=show.php?show=$file>$file</a><br>";
}
}
closedir($handle);
}
?>
show.php
Maybe I`m wrong but I think is_dir function sux...
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jan 22, 2004 1:29 pm
The problem is if(is_dir($file)) .. $file ONLY contains the filename and not the path to it. Try :
Code: Select all
<?php
$katalog = empty($_GET['katalog']) ? 'path/to/files' : $_GET['katalog'];
if ($handle = opendir($katalog)) {
while (false !== ($file = readdir($handle))) {
if(is_dir($katalog.'/'.$file))
{
echo "<a href=index.php?katalog=$file>$file</a> - directory<br>";
}
else
{
echo '<a href="show.php?show='.$katalog.'/'.$file.'">'.$file.'</a><br>';
}
}
closedir($handle);
}
?>
And change show.php to :
Code: Select all
<?php
show_source($_GET['show']);
?>
You may also want to look at
http://php.net/glob
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 2:21 pm
Thx i read about glob and will use it :], but some errors still appear line 3 maybe because I have old php ver ?
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jan 22, 2004 2:31 pm
What errors?
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 2:34 pm
Hmm now opendir can`t find the directory to open (errorno2)
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jan 22, 2004 2:40 pm
Gonna have to see your latest code
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 3:36 pm
I undo all changes i`ve made and now have the same code as it was copy from here and the same error apear Warning: OpenDir: No such file or directory (errno 2) in /public_html/show/show.php on line 3
Hmm I find and will try to use getcwd()...
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jan 22, 2004 3:39 pm
Then whatever $katalog is evaluating to doesn't exist.
So put an echo $katalog before the opendir line to debug it.
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 3:41 pm
It prints 1... :]
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jan 22, 2004 3:43 pm
Repost EXACTLY how your code looks like as it stands now
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 3:46 pm
Code: Select all
<?php
$katalog = empty($_GET['/home/mum/public_html/']);
print $katalog;
if ($handle = opendir($katalog)) {
while (false !== ($file = readdir($handle))) {
if(is_dir($katalog.'/'.$file))
{
echo "<a href=index.php?katalog=$file>$file</a> - directory<br>";
}
else
{
echo '<a href="show.php?show='.$katalog.'/'.$file.'">'.$file.'</a><br>';
}
}
closedir($handle);
}
?>
Last edited by
Citizen99 on Thu Jan 22, 2004 4:05 pm, edited 1 time in total.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jan 22, 2004 3:49 pm
empty() returns true or false, so try :
$katalog = empty($_GET['katalog']) ? '/home/mum/public_html/' : $_GET['katalog'];
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Thu Jan 22, 2004 3:55 pm
Now i get path in return but stil the error i will try chmod dirs... didn`t work but its strange i gave him path and he cant open it..
THX for help i will try to solve that in the morning.
Citizen99
Forum Commoner
Posts: 32 Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...
Post
by Citizen99 » Fri Jan 23, 2004 7:22 am
The code for now is
Code: Select all
<?php
$katalog = empty($HTTP_GET_VARS['katalog']) ? '.' : $HTTP_GET_VARS['katalog'];
print "$katalog<BR>";
if ($handle = opendir("$katalog")) {
while (false !== ($file = readdir($handle))) {
if(is_dir($file))
{
echo "<a href=km.php?show=$file>$file</a> --katalog <br>";
}
else
{
echo "<a href=me.php?show=$file>$file</a><br>";
}
}
closedir($handle);
}
?>
but still when i go one directory up or down he reads files as directory :[
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Fri Jan 23, 2004 1:56 pm
I think i said it before, but in the line, if(is_dir($file)) , $file is ONLY the filename, it contains no path information. So you need to be checking is_dir($katalog.$file) .. or possible is_dir($katalog.'/'.$file)