Page 1 of 1

open/read-dir problems...

Posted: Thu May 08, 2003 8:02 pm
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:

Posted: Thu May 08, 2003 9:22 pm
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...

Posted: Fri May 09, 2003 4:24 am
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 :)

Posted: Fri May 09, 2003 7:02 am
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:

Posted: Fri May 09, 2003 7:57 am
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.

Posted: Fri May 09, 2003 8:48 am
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