Jump to content

[SOLVED] Directory Reading Script..Gone Wrong


AbydosGater

Recommended Posts

Hi, Just working on a directory reading script..But the output is all messed up.

Here is the code:

<?php
$dp = opendir("/apache2triad/");
$files = array();
$dirs = array();
$unknowns = array();
while (false !== ($file = readdir($dp))){
if (is_file($file)){
	$files[] = $file;
} elseif (is_dir($file)){
	$dirs[] = $file;
} else {
$unknowns[] = $file;
}
}
echo '<strong>Files:</strong><br />';
foreach ($files as $current){
echo $current.'<br />';
}
echo '<br /><hr /><br />';
echo '<strong>Directorys:</strong><br />';
foreach ($dirs as $current){
echo $current.'<br />';
}
echo '<br /><hr /><br />';
echo '<strong>Unknown Files:</strong><br />';
foreach ($unknowns as $current){
echo $current.'<br />';
}
echo '<br /><hr /><br />';
?>

 

But All the basic files like the "htdocs" file and the directorys like "." and ".." and being saved right.. But everything else.. ALL other files and folders are being saved as unknowns... can anyone see what im doing wrong? Please?

 

Thanks Andy

When you use is_dir or is_file you will also want to define the path where those files/directory exists. If you don't define the path then it will look in the directory the script is being ran in. It wont check the directory you are reading.

 

Try this:

<?php

define('DIR', '../Apache/');

$dp = opendir(DIR);

$files = array();
$dirs = array();
$unknowns = array();

while (false !== ($file = readdir($dp)))
{
    if (is_file(DIR . $file))
    {
        $files[] = $file;
    }
    elseif (is_dir(DIR . $file))
    {
        $dirs[] = $file;
    }
    else
    {
        $unknowns[] = $file;
    }
}

echo '<strong>Files:</strong><br />';
foreach ($files as $file)
{
    echo $file.'<br />';
}

echo '<br /><hr /><br />';

echo '<strong>Directorys:</strong><br />';
foreach ($dirs as $dir)
{
    echo $dir.'<br />';
}

echo '<br /><hr /><br />';

echo '<strong>Unknown Files:</strong><br />';
foreach ($unknowns as $unkown)
{
    echo $unkown.'<br />';
}

echo '<br /><hr /><br />';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.