Page 1 of 2
Listing
Posted: Thu Jan 22, 2004 1:14 pm
by Citizen99
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...
Posted: Thu Jan 22, 2004 1:29 pm
by markl999
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
Posted: Thu Jan 22, 2004 2:21 pm
by Citizen99
Thx i read about glob and will use it :], but some errors still appear line 3 maybe because I have old php ver ?
Posted: Thu Jan 22, 2004 2:31 pm
by markl999
What errors?
Posted: Thu Jan 22, 2004 2:34 pm
by Citizen99
Hmm now opendir can`t find the directory to open (errorno2)
Posted: Thu Jan 22, 2004 2:40 pm
by markl999
Gonna have to see your latest code

Posted: Thu Jan 22, 2004 3:36 pm
by Citizen99
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()...
Posted: Thu Jan 22, 2004 3:39 pm
by markl999
Then whatever $katalog is evaluating to doesn't exist.
So put an echo $katalog before the opendir line to debug it.
Posted: Thu Jan 22, 2004 3:41 pm
by Citizen99
It prints 1... :]
Posted: Thu Jan 22, 2004 3:43 pm
by markl999
Repost EXACTLY how your code looks like as it stands now

Posted: Thu Jan 22, 2004 3:46 pm
by Citizen99
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);
}
?>
Posted: Thu Jan 22, 2004 3:49 pm
by markl999
empty() returns true or false, so try :
$katalog = empty($_GET['katalog']) ? '/home/mum/public_html/' : $_GET['katalog'];
Posted: Thu Jan 22, 2004 3:55 pm
by Citizen99
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.
Posted: Fri Jan 23, 2004 7:22 am
by Citizen99
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 :[
Posted: Fri Jan 23, 2004 1:56 pm
by markl999
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)
