Hello ...I just was trying to output the file names of different folders in my PC using PHP code...But I found that it outputs actually more than visible files in that particular folder..So what did I mean by visible that when I count the number of files in my folder it appears 18 but when i output it appears that the numer is 20. That two files are not visible. One if the is "Folder.jpg" and the second of them is a ".doc" file i opened and think it is inserted by "Administrator".....This is the code

<?php

 function getDirectoryList ($directory)  
  { 

	// create an array to hold directory list 
	$results = array(); 

	// create a handler for the directory 
	$handler = opendir($directory); 

	// open directory and walk through the filenames 
	while ($file = readdir($handler)) { 

	  // if file isn't this directory or its parent, add it to the results 
	  if ($file != "." && $file != "..") { 
		$results[] = $file; 
	  } 

	} 

	// tidy up: close the handler 
	closedir($handler); 

	// done! 
  return $results; 

  } 
  $array_1=getDirectoryList("C:\Documents and Settings\Администратор\Мои документы\Downloads"); 

  
 foreach($array_1 as $value) 
 { 
 echo "<br/>".$value; 
 }
?>

So here is my question ...how can I output only that 18 files and not that invisible files ? Since I want to work only with files that I see.

Please be gentle and help me.

Recommended Answers

All 11 Replies

This code works in windows.
I have used exec function to run attrib command to check type of file.
In linux os attrib won't work.

<?php

 function getDirectoryList ($directory)  
  { 

	// create an array to hold directory list 
	$results = array(); 

	// create a handler for the directory 
	$handler = opendir($directory); 

	// open directory and walk through the filenames 
	while ($file = readdir($handler)) { 

	  // if file isn't this directory or its parent, add it to the results 
	  if ($file != "." && $file != "..") { 
	  	$str = $directory.'\\'.$file;
		$out = trim(exec('attrib '.$str));
		$start = substr($out,0,1);
		if($start != 'H')
		$results[] = $file; 
	  } 

	} 

	// tidy up: close the handler 
	closedir($handler); 

	// done! 
  return $results; 

  } 
  $array_1=getDirectoryList("C:\wamp\www\\test"); 

  
 foreach($array_1 as $value) 
 { 
 echo "<br/>".$value; 
 }
?>

Try this and let me know if it works.

Thx for the help. But the code didnt work..the same result.

But you know i tried my initial code for couple other directories and found that it worked for that cases ...Maybe my download section is kinda speacial thats why i have that 2 more outputs..What do you think?

Ohh..
i have checked only for hidden folder. Below is the complete code for files and folder both.
I have checked in win7 and its working. Try in your PC.

<?php

 function getDirectoryList ($directory)  
  { 

	// create an array to hold directory list 
	$results = array(); 

	// create a handler for the directory 
	$handler = opendir($directory); 

	// open directory and walk through the filenames 
	while ($file = readdir($handler)) { 

	  // if file isn't this directory or its parent, add it to the results 
	  if ($file != "." && $file != "..") { 
	  	$str = $directory.'\\'.$file;
		$out = trim(exec('attrib '.$str));		
		$ar = explode($directory,$out);		
		$temp = explode(' ',trim($ar[0]));		
		if(!in_array('H',$temp))
		$results[] = $file; 
	  } 

	} 

	// tidy up: close the handler 
	closedir($handler); 

	// done! 
  return $results; 

  } 
  $array_1=getDirectoryList("C:\wamp\www\\test"); // add your path here

  
 foreach($array_1 as $value) 
 { 
 echo "<br/>".$value; 
 }
?>

Replace your directory path.

vibhadevit it again outputs the same. When you say it is working on windows 7 you mean that you have invisible files in a directory and the function doesnt output that files , right? My operating system is Windows XP Professional and I have installed XAMPP. Maybe this could help

Member Avatar for diafol

when you say files are invisible, what do you mean? not visible from the windows explorer? Have they got certain file properties? Find out.

I mean that when I visit that directory i see that files but when output them using PHP it outputs more than i can see in that folder.

Are you meaning hidden files ?

Member Avatar for diafol

Again:

Have they got certain file properties? Find out.

Unless you know what properties those special files have, it's difficult to know what's going on. As Zero states,they're probably hidden files. But we need to be sure.

To show hidden files:


I don't know how php handles 'hidden' files. I was wondering about permissions, but I don't think this will work. I looked at the stat() function and the inode mode, but it doesn't change for hidden or visible files on windows.

Member Avatar for diafol

Perhaps using system() and the 'attrib', but I don't know if this will allow a 'get'. Everything I've seen on it is 'set'.

$file = 'filename.php';
system('attrib +H ' . escapeshellarg($file));

That's pretty much the way to make a file hidden OR:

$file = 'filename.php';
system('attrib -H ' . escapeshellarg($file));

TO 'unhide' a file.

Couldn't find anything on returning the attribute as opposed to setting it. Surely it must exist.

@ardav: I have made one file and one directory hidden by right click and changing property to hidden.
And used exec to read file/folder attributes in PHP as shown in above post..
exec('attrib '.$path)

Member Avatar for diafol

Ok, I got this to check to see if the file is hidden:

<?php
$file = 'file.php';
$vis = explode(" ",exec('attrib ' . $file));
//print_r($vis);
if($vis[3] == 'H') echo "yep $file is hidden";
?>

I haven't tested whether H crops up at any other position. Anyway play with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.