Current location: Hot Scripts Forums » Programming Languages » PHP » var_dump(is_dir($str)); displaying wrong info


var_dump(is_dir($str)); displaying wrong info

Reply
  #1 (permalink)  
Old 06-29-09, 05:27 PM
Mass Zero's Avatar
Mass Zero Mass Zero is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Amsterdam, NL
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
var_dump(is_dir($str)); displaying wrong info

Hey,

I was doing a small file listing script and I wanted to exclude subdirectories from being shown. For that, I used:

PHP Code:

if(is_dir($file)) {

continue;

This isn't working though. After checking with var_dump(is_dir($file));, it seems subdirectories aren't being recognized as directories, since it returned false.

Anyone know how I can fix this?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 06-30-09, 02:38 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Can you post some more code? I'm guessing $file doesn't actually contain the expected value.
Reply With Quote
  #3 (permalink)  
Old 07-01-09, 07:23 AM
Mass Zero's Avatar
Mass Zero Mass Zero is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Amsterdam, NL
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

    $dir 'random/dir/here';

    
$result = array();
    if(
is_dir($dir)) {
        if(
$opendir opendir($dir) or die('Unable to open ' $dir '.')) {
            while(
$file readdir($opendir)) {
                if(
is_dir($file)) {
                    continue;
                }
                elseif(
$file != '.' && $file != '..') {
                    
$result[] = $file;
                }
            }
            
closedir($opendir);
            
sort($result);
            for(
$loop 0; isset($result[$loop]); $loop++) {
                echo 
$result[$loop] . '<br />';
            }
        }
    } 
Reply With Quote
  #4 (permalink)  
Old 07-01-09, 07:37 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
While var_dump() is a nice debugging function, a simple "echo" would probably have led you to the solution.

PHP Code:

if (is_dir($dir '/' $file))

    continue; 
... although, I'd save the is_dir/continue/elseif part, by doing it this way:
PHP Code:

if (is_file($dir '/' $file))

    
$result[] = $file
PHP Code:

<?php


$dir 
'random/dir/here';
$result = array();

if (
is_dir($dir))
{
    if (
$opendir opendir($dir) or die('Unable to open ' $dir '.'))
    {
        while ((
$file readdir($opendir)) !== false)
        {
            if (
is_file($dir '/' $file))
                
$result[] = $file;
        }
        
        
closedir($opendir);
        
sort($result);

        echo 
implode("<br />\n"$result);
    }
}  


?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pull down menu populated by database info - displaying selected option bluedogsb PHP 3 06-26-09 05:25 PM
Creating ACcounts and Displaying Account Info Zan Script Requests 3 06-29-06 10:10 AM
cascade info script? zeon Script Requests 0 05-23-04 11:42 PM
reading info from a file (csv / xls) and displaying info lordmerlin PHP 7 03-20-04 03:39 PM
not displaying the correct info sparky PHP 8 06-26-03 08:28 AM


All times are GMT -5. The time now is 08:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.