open/read-dir problems...

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

Post Reply
User avatar
marx
Forum Newbie
Posts: 3
Joined: Thu May 08, 2003 8:02 pm

open/read-dir problems...

Post by marx »

Hi there. Got some serious problems with the following code. I just want to list the filenames and filesizes in the subdirectory "dummy" but get an error on line 2, $open = opendir("dummy"); - what´s the story? Have tried dummy/ - /dummy/ - and all kinds of things...

Code: Select all

<?php 
$open = opendir ("dummy");
while ($files = readdir ($open)) &#123;
$filename = .$files;
	if (is_file ($filename)) &#123;
	$size = filesize ("$files");

	print "$files - $size <br>";
	&#125;
&#125;
closedir ($open);

?>
Truly grateful for all help. Respect Massive.

MARX :twisted:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

only cause i can spot is
$filename = .$files;
maybe you wanted to prepend the directory name and forgot to remove the dot when deleting the $dir (or whatever) variable ;)
But that's on line 4...
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

For the filesize:

Code: Select all

function calfilesize($size) 
  &#123; 
    $name = "&nbsp;b"; 
    if($size>1024) 
    &#123; 
      $size = $size/1024; 
      $name = "Kb"; 
    &#125; 
    if($size>1024) 
    &#123; 
      $size = $size/1024; 
      $name = "Mb"; 
    &#125; 
    if($size>1024) 
    &#123; 
      $size = $size/1024; 
      $name = "Gb"; 
    &#125; 
//    $ret = round($size)." ".$name; 
    $ret = sprintf("%01.2f", $size)."&nbsp;".$name; 
    return $ret; 
  &#125;
It's not the best script, but it works :)
User avatar
marx
Forum Newbie
Posts: 3
Joined: Thu May 08, 2003 8:02 pm

Post by marx »

Thanks, guys. Did some modifications to the script, and now it manages to open the dir, but don´t print the filenames! It gets the right number of files though...

Another thing; the "Parse error on line number X"-message keeps on changeing although I haven´t have made changes to the script! The X changes. Is that common or just me?!

bigUps. MARX :twisted:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
$path = '.';
$open = opendir($path);
$path .= '/';
while ($file = readdir($open)) {
	$afname = $path.$file;
	if (is_file ($afname)) {
		$size = filesize($afname);
		print "$afname - $size <br />";
	}
}
closedir ($open);
?>
this one works for me without errors.
User avatar
marx
Forum Newbie
Posts: 3
Joined: Thu May 08, 2003 8:02 pm

Post by marx »

Finally managed to get the code working on my own, but found some new goodies in that last snippet of coda that Volka posted. However, that one also listed the "dummy/.DS_Store - 6148"-file. Also found an old post telling how to sort by date. Wicked.

Thanks a bunch!

MARX
Post Reply