Jump to content

[SOLVED] get directory items


HektoR

Recommended Posts

hi all.

 

i trying to get directory items but unsuccessfully .

i made this code:

 

<?php 

$dir = "C:\Program Files\VertrigoServ\www";

if(is_dir($dir))
{
$dh = opendir($dir);
while($file = readdir($dh) !== false)
{
	echo "filenme :" . $file . "filetype : " . filetype($file);
}
}else
{
echo "can not find dir";
echo $dir;
}

 

and receive that  errors:

 

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

filenme :1filetype :

Link to comment
https://forums.phpfreaks.com/topic/140128-solved-get-directory-items/
Share on other sites

.... and it will be like

 

$dir = "C:/Program Files/VertrigoServ/www";

if(is_dir($dir))
{
$dh = opendir($dir);
while($file = readdir($dh) !== false)
{
	filetype($file) . "\n";
}
}else
{
echo "can not find dir";
echo $dir;
}

 

i again received error:

Warning: filetype() [function.filetype]: Lstat failed for 1 in C:\Program Files\VertrigoServ\www\test\readdir.php on line 10

This has nothing to do with the direction of the slashes. You have two problems. Your first is operator precedence.

 

The !== operator has higher precedence than the = operator, so it is evaluated first. The result of readdir($dh) !== false is then assigned to $file. So because that is true for each iteration, 1 is assigned to $file.

 

Your second problem is that you need to append the file name to the directory:

 

 

<?php 

$dir = "C:\Program Files\VertrigoServ\www";

if(is_dir($dir))
{
   $dh = opendir($dir);
   while(($file = readdir($dh)) !== false)
   {
      echo "filename :" . $file . "filetype : " . filetype($dir.'\\'.$file);
   }
}else
{
   echo "can not find dir";
   echo $dir;
}

Why don't you use the glob function? It doesn't return directory entries "." and "..":

<?php
$dir = "C:/Program Files/VertrigoServ/www";
if (is_dir($dir))
   foreach (glob($dir . '/*') as $file)
      echo "filename :" . $file . "filetype : " . filetype($file) . "<br>\n";
else {
echo "can not find dir";
echo $dir;
}
?>

 

Much easier to write (IMHO).

 

Ken

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.