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

Link to comment
Share on other sites

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 />';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.