Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,582 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,931 people online right now. Registration is fast and FREE... Join Now!




file explorer

 
Reply to this topicStart new topic

file explorer

ajaycode
29 Sep, 2008 - 11:49 PM
Post #1

New D.I.C Head
*

Joined: 9 Aug, 2008
Posts: 42

i wrote file explrer just displays directories but doesnt shows file info pls help me
CODE

<?
function ContentsOfDir($path)
{
    $path = realpath($path); #create real path
    $dirhandle = @opendir($path); #create directory class
    $res=array();//aj-----
    $count = 0;
    $dirs="";
    $files="";
    
    #check every item in the directory
    //while( false !== @($item = $dirhandle->read()) )
    while (false !== @($item = readdir($dirhandle)))
    {
        #exlcude forbidden files/dirs
      if( $item != '.' AND $item != '..' )
          {
            #check if dir or file
            if( is_dir($path.'/'.$item) )
            {
                #check if its not banned
                //if(!isBannedDir($path.'/'.$item)){
                    $dirs[] = $item;
                    
            }
        }
        elseif( is_file($path.'/'.$item) )
            {
                $files[] = $item;
                $dirs[] = $item;
            }
            $count++;
    }

    
    //$dirhandle->close();

if( is_array($dirs) )
    {
        usort($dirs, 'strnatcmp');
    }
    if( is_array($files) )
    {
        usort($files, 'strnatcmp');
    }
    #create result array
    $res['items'] = $count;
    $res['dirs'] = $dirs;
    $res['files'] = $files;
    return     $res;
}

function ItemsInDir($dir)
{
    $res = ContentsOfDir($dir);
    if( is_array($res['files']) )
    {
        $items = count($res['files']);
    }
    else
    {
        $items = 0;
    }
    if( is_array($res['dirs']) )
    {
        for( $i=0; $i < count($res['dirs']); $i++ )
        {
            $items = $items + ItemsInDir($dir.'/'.$res['dirs'][$i]);
        }
    }
    return $items;
}


function ExtractExt($filename)
{
    if(($dotpos = strrpos($filename, '.')) === false)
    {
    return false;
    }
    else
    {
    return substr($filename, $dotpos+1);
    }
}

function FileDescriptionGen($filename)
{
    $ext = strtolower(ExtractExt($filename));
    if($ext == 'png' OR $ext == 'gif' OR $ext == 'jpg' OR $ext == 'psp' OR $ext == 'bmp' OR $ext == 'ai' OR $ext == 'tiff')
    {
    $res['picfilename'] = 'pic.gif';
        $res['description'] = strtoupper($ext).' Image/Picture';
  }
  elseif($ext == 'html' OR $ext == 'htm')
  {
    $res['picfilename'] = 'doc.gif';
        $res['description'] = 'Webpage';
  }
  elseif($ext == 'css')
  {
    $res['picfilename'] = 'txt.gif';
        $res['description'] = 'Stylesheet';
  }
  elseif($ext == 'doc' OR $ext == 'pdf')
  {
    $res['picfilename'] = 'doc.gif';
        $res['description'] = 'Document';
  }
  elseif($ext == 'php' OR $ext == 'php3')
  {
    $res['picfilename'] = 'php.gif';
        $res['description'] = 'PHP Script';
  }
  elseif($ext == 'js')
  {
    $res['picfilename'] = 'txt.gif';
        $res['description'] = 'Javascript';
  }
  elseif($ext == 'swf')
  {
    $res['picfilename'] = 'pic.gif';
        $res['description'] = 'Flash file';
  }
  elseif($ext == 'txt')
  {
    $res['picfilename'] = 'txt.gif';
        $res['description'] = 'Textfile';
  }
  
  elseif($ext == 'avi' OR $ext == 'mov' OR $ext == 'mpg' OR $ext == 'rm')
  {
    $res['picfilename'] = 'mov.gif';
        $res['description'] = 'Video file';
  }
  elseif($ext == 'mp3' OR $ext == 'wav' OR $ext == 'ogg')
  {
    $res['picfilename'] = 'muz.gif';
        $res['description'] = 'Music file';
  }
  elseif($ext == 'zip' OR $ext == 'rar' OR $ext == 'cab' OR $ext == 'b2z')
  {
    $res['picfilename'] = 'zip.gif';
        $res['description'] = 'Compressed file';
  }
  elseif($ext == 'exe' OR $ext == 'bat')
  {
    $res['picfilename'] = 'exe.gif';
        $res['description'] = 'Application';
  }
  else
  {
    $res['picfilename'] = 'enz.gif';
        $res['description'] = 'File';
  }
    return $res;
}




#execute Script
//$phpself = $_SERVER['PHP_SELF'];
$dir = "e:/";
$userroot = "root";
$fulldir = ($dir);
//$res=ItemsInDir('PHP');
?>

<!------------javascript-------------->

<script type="text/javascript">
function open_dir(path)
{
    document.forms[0].path.value = path;
    document.forms[0].target = "_self";
    document.forms[0].submit();
}
    
    //$res=ItemsInDir('PHP');
</script>
<?
/*
$userroot='';
//$fulldir='';
       echo '<form method="post" target="_self">';
    echo '<input type="hidden" name="path" value="'.$dir.'">';    
if( substr($fulldir, 0, strlen($userroot)) != $userroot)
{
    #dir is lower than the root dir, show error
    echo '<div style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; margin-top: 5px; margin-left: 5px;">Permission           denied to access this directory. Contact system administrator to set access to this directory</div>';
}
elseif( !is_dir($fulldir) )
{
    echo '<div style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; margin-top: 5px; margin-left: 5px;"><font                color=red>**The requested directory does not exist. Contact system administrator to set access to your home directory.          </font></div>';
}
else
{
    echo '<table cellpadding="0" cellspacing="0" style="font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 8pt;" width="100%">
    <tr>
        <td style="width: 20px;"></td>    
        <td style="width: 175px;"></td>
        <td style="width: 45px;"></td>
        <td style="width: 70px;"></td>
        <td style="width: 70px;"></td>
    </tr>';
*/
    if($fulldir != $userroot)
    {
        $upperdir = "";
        #haal de juiste upper dir locatie op
        $split = explode('/', $dir);
        if( $split[count($split)-1] == '' )
        {
            #haal de lege dir uit de lijst
            array_pop($split);
        }
        if( count($split)==1 )
        {
            $upperdir = '';
        }
        else
        {
            for( $i=0; $i < count($split)-1; $i++ )
            {
                $upperdir = $upperdir.$split[$i].'/';
            }
        }
     }
     /*
     echo'<tr bgcolor="#999999">
                <td style="text-align: right;"><img src="images/updir.gif" style="width: 16px; height: 14px; border: none;" alt=""
                align="absmiddle" /></td>
                <td height=20 onclick="java script:open_dir(\''.$upperdir.'\');" style="cursor: pointer;"><b> <u>Parent                         Directory</u></b></td>
                <td></td>
                <td>  </td>
               <td style="width: 70px;"></td>
              <td style="text-align: right; width: 70px;"></td>            
        </tr>';  
}
*/
#get contents of dir
    $res = ContentsOfDir($fulldir);
    //print_r($res['dirs']);

#list dirs
if( is_array($res['dirs']) )
{
    for( $i=0; $i < count($res['dirs']); $i++)
    {
    $dirname = $res['dirs'][$i];

    echo '<tr bgcolor="#CCCCCC">

                <td style="text-align: right;"><img src="images/icon_dir.gif" style="width: 16px; height: 14px; border: none;"                                                                            alt="" align="absmiddle" /></td>
                <td onclick="java script:open_dir(\''.$dir.$dirname.'/\');" style="cursor: pointer;"                                                          onmouseover="this.style.backgroundColor=\'#2B6CFF\'; this.style.color=\'white\';"                                                   onmouseout="this.style.backgroundColor=\'\';  
                this.style.color=\'\';"><b> '.$dirname.'</b></td>
                <td></td>
                <td style="text-align: right; width: 120px;">Directory ('.ItemsInDir($fulldir.'/'.$dirname).' items)                      </td>
                <td style="width: 70px;"></td>
                <td style="text-align: right; width: 70px;">';    
    }    
}


#list files
if( is_array($res['files']) )
{
    for( $i=0; $i < count($res['files']); $i++)
    {
    $filename = $res['files'][$i];
    $filepath = $fulldir.'/'.$filename;
    $desc = FileDescriptionGen($filepath);
    echo '<tr bgcolor="#CCCCCC">
                <td style="text-align: right;"><img src="images/'.$desc['picfilename'].'" style="width: 16px; height: 14px; border:                 none;" alt="" align="absmiddle" /></td>
                <td width="45%" style="cursor: pointer;" onmouseover="this.style.backgroundColor=\'#2B6CFF\'; this.style.color=
                \'white\';" onmouseout="this.style.backgroundColor=\'\'; this.style.color=\'\';"> '.$filename.' </td>';
        
    $set=ItemsInDir($filename);
    return $set['files'];
    }
}    

?>




User is offlineProfile CardPM
+Quote Post

no2pencil
RE: File Explorer
29 Sep, 2008 - 11:52 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Are you sure that the file/folder permissions are set correctly?

In other words, does the process that is running your web server have authorization to go into those directories & view the files?
User is offlineProfile CardPM
+Quote Post

ajaycode
RE: File Explorer
30 Sep, 2008 - 02:16 AM
Post #3

New D.I.C Head
*

Joined: 9 Aug, 2008
Posts: 42

QUOTE(no2pencil @ 30 Sep, 2008 - 12:52 AM) *

Are you sure that the file/folder permissions are set correctly?

In other words, does the process that is running your web server have authorization to go into those directories & view the files?



thanks for response

all are correct permissions

help me
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: File Explorer
30 Sep, 2008 - 02:25 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
remove the error suppressors and run the code again. That might help you to see what exactly the error/warning is.
User is online!Profile CardPM
+Quote Post

Hary
RE: File Explorer
30 Sep, 2008 - 03:07 AM
Post #5

D.I.C Head
**

Joined: 23 Sep, 2008
Posts: 205



Thanked: 15 times
My Contributions
Why do you start a new thread for your explorer? You didnt like the answers in the first one?

Anyway, the error is in this piece of code:
CODE

if( $item != '.' AND $item != '..' )
          {
            #check if dir or file
            if( is_dir($path.'/'.$item) )
            {
                #check if its not banned
                //if(!isBannedDir($path.'/'.$item)){
                    $dirs[] = $item;
                    
            }
        }
        elseif( is_file($path.'/'.$item) )
            {
                $files[] = $item;
                $dirs[] = $item;
            }
            $count++;
    }


The elseif( is_file($path.'/'.$item) ) is bound to the
if( $item != '.' AND $item != '..' ), you have a misplaced }
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:37AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month