Jump to content

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

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.