Jump to content

  • Log In with Google      Sign In   
  • Create Account


[web] PHP file / dir list


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 BASICFreak   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 June 2010 - 09:06 AM

Ok, I have made a data server and did not like the design of apache's default index so I made my own (simple one) here are the codes:

Root Directory index.php:
<?php
$thelist .= '<html><body bgcolor=black text=white link=yellow vlink=yellow><center><table border=1 width=800><tr><td width=75%>File Name</td><td width=18%>File Size</td><td width=7%></td></tr>';
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != "index.php")
{
if(!is_dir($file))
{
$size = sprintf("%u", filesize($file));
$type = "B";
if($size < 0)
{
$size = $size * -1;
}
if($size >= 1024)
{
$size = $size / 1024;
$type = "KB";
$size = number_format(round($size, 2), 2);
}
if($size >= 1024)
{
$size = $size / 1024;
$type = "MB";
$size = number_format(round($size, 2), 2);
}
if($size >= 1024)
{
$size = $size / 1024;
$type = "GB";
$size = number_format(round($size, 3), 3);
}
$thelist .= '<tr><td><a href="'.$file.'">'.$file.'</a></td><td>'.$size.'</td><td>'.$type.'</td></tr>';
}
if(is_dir($file))
{
$size="";
$type="DIR";
$thelist .= '<tr><td><a href="'.$file.'">'.$file.'</a></td><td>'.$size.'</td><td>'.$type.'</td></tr>';
}
}
}
closedir($handle);
}
$thelist .= '</table></center></body></html>';

?>
<P><?=$thelist?></p>

Sub-Directory index.php:
<?php
$thelist .= '<html><body bgcolor=black text=white link=yellow vlink=yellow><center><table border=1 width=800><tr><td width=75%>File Name</td><td width=18%>File Size</td><td width=7%></td></tr>';
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "index.php")
{
if(!is_dir($file))
{
$size = sprintf("%u", filesize($file));
$type = "B";
if($size < 0)
{
$size = $size * -1;
}
if($size >= 1024)
{
$size = $size / 1024;
$type = "KB";
}
if($size >= 1024)
{
$size = $size / 1024;
$type = "MB";
}
if($size >= 1024)
{
$size = $size / 1024;
$type = "GB";
}
$size = number_format(round($size, 2), 2);
$thelist .= '<tr><td><a href="'.$file.'">'.$file.'</a></td><td>'.$size.'</td><td>'.$type.'</td></tr>';
}
if(is_dir($file))
{
$size="";
$type="DIR";
$thelist .= '<tr><td><a href="'.$file.'">'.$file.'</a></td><td>'.$size.'</td><td>'.$type.'</td></tr>';
}
}
}
closedir($handle);
}
$thelist .= '</table></center></body></html>';

?>
<P><?=$thelist?></p>


I am just posting this for anyone who is interested

I also want to know is there a limit in file size (I think it is 4GB)

Sponsor:

#2 Sander   Members   -  Reputation: 1328

Like
0Likes
Like

Posted 10 June 2010 - 07:57 PM

Quote:
I also want to know is there a limit in file size (I think it is 4GB)


That depends on the underlying file system. On XFS the maximum file size is 8 exabyte (16 terrabyte on 32-bit systems). On ZFS, maximum file size is 16 exabyte. That's 16 billion gigabyte, or 16 million terrabyte.

#3 phresnel   Members   -  Reputation: 949

Like
0Likes
Like

Posted 10 June 2010 - 08:29 PM

You have a total maintenance nitemare over there.

The two files are almost identical and hell redundant, why have different scripts for basically the same task?

Then, why not use loops in the inner code?

(p-code)
struct Unit {
const string postfix;
const int factor;
};
Unit units [] = {
{ "B", 1 },
{ "KiB", 1024 },
{ "MiB", 1024*1024 },
{ "GiB", 1024*1024*1024 },
};


and then find for which ($size/factor)<1.


Finally, you'll find this interesting.

#4 BASICFreak   Members   -  Reputation: 115

Like
0Likes
Like

Posted 11 June 2010 - 03:46 AM

Quote:
Original post by Sander
Quote:
I also want to know is there a limit in file size (I think it is 4GB)


That depends on the underlying file system. On XFS the maximum file size is 8 exabyte (16 terrabyte on 32-bit systems). On ZFS, maximum file size is 16 exabyte. That's 16 billion gigabyte, or 16 million terrabyte.


I am talking about the script and what it can read, it was anything over 2gb was negative and i haven't yet got any higher since putting in 'sprintf("%u", filesize($file));'. I am using INT for bytes of data and the number is fairly large, I was looking for a way to use a FLOAT value but couldn't find any good sources.

The reason I have two scripts is, I tried with just one and when I change directories any sub-sub-directories are not accessible it will not detect them as a dir. (the second script allows "..")

Yes it could be neater, but I just wanted something that works.

#5 Wan   Members   -  Reputation: 1366

Like
0Likes
Like

Posted 11 June 2010 - 05:31 AM

Quote:
Original post by Thoover
couldn't find any good sources

I haven't looked at every one of them, but is there nothing in the user contributions of the PHP manual that might help?

#6 Sander   Members   -  Reputation: 1328

Like
0Likes
Like

Posted 12 June 2010 - 11:40 AM

Quote:
I am talking about the script and what it can read, it was anything over 2gb was negative and i haven't yet got any higher since putting in 'sprintf("%u", filesize($file));'. I am using INT for bytes of data and the number is fairly large, I was looking for a way to use a FLOAT value but couldn't find any good sources.


The problem is PHP's filesize() function. Better use something like this to get the file size (This is for Linux. There are windows versions in the stat() and filesize() comments on php.net):

size = exec ('stat -c %s '. escapeshellarg ($path));


You'll get the result as a string. Use bcmath to convert it (bcmath does big numbers as strings). That way you can calculate up to the theoretical limit of 16 exabytes.

It's not that you'll really need 16 exabytes, but files over 2 or 4 gigabyte are becoming quite common. The average DVD ISO is larger than that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS