[web] PHP file / dir list

Started by
4 comments, last by Sander 13 years, 10 months ago
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)
Advertisement
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.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

This topic is closed to new replies.

Advertisement