du2.php

This is based off of du.php, and is basically the same thing except it lets you click on folders to navigate through the directory structure and see how much space each directory is taking up.

It uses php’s shell_exec function to call the du utility on the directories it’s in. So if your host doesn’t allow you to use shell_exec, this isn’t going to work for you. If they do allow shell_exec, but do not allow ssh access then this is perfect.


You can view it here as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<HTML><HEAD><TITLE>Disk Usage Report</TITLE></HEAD><BODY>
<?php
///////////////////////////////////////////////////////////
//Disk Usage script v2                       ///
//du2.php                          ///
/////////////                         ///
//Author:  Justyn Shull                    ///
//E-mail:  justyn@justynshull.com           ///
/////////////                      ///
//based off my old du.php script:         ///
//This lists the directories in $dir with the   ///
//space they take up.  Not sorted, but allows  ///
//you to click on a directory to navigate     ///
////////////////////////////////////////////////
 
/////TODO: Fix it so you don't end up with the full path you've taken
//        eg:   du2.php?dir=../mail/../html/
 
$time1 = microtime(True);
chdir(getenv('DOCUMENT_ROOT'));
chdir("..");
$homedir = shell_exec('pwd');
echo "Home: $homedir";
if ($_GET["dir"] == "")
    //$dir = getenv('DOCUMENT_ROOT')."/.."; // *should* be the users's home
    $dir = ".";
else
    $dir = $_GET["dir"];
 
echo escapeshellarg($dir)."<br />";
chdir($dir);
echo shell_exec("pwd")."<br />";
$homeindir = strpos(shell_exec("pwd"),$homedir);
//if ($homeindir === false) die ("Invalid dir");  //hopefully this will keep us from leaving $home
 
exec('ls -la',$dirs,$rc);
if ($rc != 0) die("Error listing files");
$numdir = count($dirs) - 1;
for ($x = 1;$x <= $numdir; $x++){
    $type = substr($dirs[$x],0,1);
    if ($type == "d") {
    $lsdirs[] = substr(strrchr($dirs[$x]," "),1);
    }
}
$numdir = count($lsdirs) - 1;
 
// Print usage now
echo "<br />";
for ($x = 0; $x <= $numdir; $x++) {
    if ($x == 1) {
        echo "<a href=du2.php?dir=$dir/$lsdirs[$x]>";
        echo "<img src=\"/icons/folder.gif\" border=0></a>    ..<br />";
    } else {
    unset($du);
    exec("du -hs ".escapeshellarg($lsdirs[$x]),$du,$rc);
    if ($rc != 0) die("Error on ".$x.": ".escapeshellarg($lsdirs[$x]));
    echo "<a href=du2.php?dir=$dir/$lsdirs[$x]>";
    echo "<img src=\"/icons/folder.gif\" border=0></a>".$du[0]."<br />";
    }
}
 
 
$time2 = microtime(True); 
$dutime = ($time2 - $time1); 
echo '<p>Page generated in <b>'.$dutime.'</b> seconds</p>';
?>
 
</BODY></HTML>

Tags: , ,

3 Responses to “du2.php”

  1. James R Says:

    Hi there. I am having a bit of a problem with the script, I’m guessing it’s some sort of permissions issue. I can browse some directories, but others come up blank and won’t show the subdirectories – with error “Error on 2: ‘Existence”. Could you help me out?

    Thanks.

  2. admin Says:

    Hey James, does the user the script’s being run as have permission to access the directories your getting errors on? Read and Execute should be all it would need. Does the php error_log have anything useful in it when it errors out? If you want to send me an e-mail (justyn [at] justynshull.com) I wouldn’t mind helping you troubleshoot it.

    I have a different file manager script that does what this one does and some more that I’ll be posting soon. It actually has some error checking in it and may be more useful since it won’t just die without saying why on directories it can’t access.

  3. Ryan Uber Says:

    I had a quick suggestion on this post — You could do away with a lot of the shell_exec-type functions by doing something like what this guy is doing:

    http://www.phpro.org/examples/Directory-Size.html

    and instead of using exec ( “pwd” ), use getcwd() instead. Then, for the directory listing, you could use a combination of opendir() to get the directory handle open, and then throw a while loop onto a readdir() and loop through the listing. I think you could pretty much clear up all of the exec stuff in this script, then it would not matter if you had exec-functionality enabled in php.ini.

    Don’t forget to throw an is_readable() if statement in there or something so that unreadable directories or files won’t stop your show.

    Good work on this script though. I can see this being handy for anyone with web hosting somewhere who doesn’t understand where all of their quota went, and no SSH access.

Leave a Reply