Listing

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...

Listing

Post 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")) &#123;
      
while (false !== ($file = readdir($handle))) &#123; 
if(is_dir($file)) 
&#123;
echo "<a href=index.php?show=$file>$file</a> - directory<br>";  
&#125;
else
&#123;
echo "<a href=show.php?show=$file>$file</a><br>";
&#125;
  &#125;
 closedir($handle); 
 &#125;
 ?>
show.php

Code: Select all

<?
show_source($show);
?>
Maybe I`m wrong but I think is_dir function sux...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
Citizen99
Forum Commoner
Posts: 32
Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...

Post 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 ?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What errors?
Citizen99
Forum Commoner
Posts: 32
Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...

Post by Citizen99 »

Hmm now opendir can`t find the directory to open (errorno2)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

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 »

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()...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

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 »

It prints 1... :]
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Repost EXACTLY how your code looks like as it stands now :o
Citizen99
Forum Commoner
Posts: 32
Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...

Post 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); 
} 
?>
Last edited by Citizen99 on Thu Jan 22, 2004 4:05 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

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 »

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 »

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 :[
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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)

:o
Post Reply