Jump to content

is_dir() hiccup


Brian W

Recommended Posts

I'm just messin around with the dir() functions to learn how they work and how they could be helpful and encountered a problem.

Not all of the directories are being caught by if(is_dir($filename) == TRUE)... so some of them are being treated like files. Here is my code.

<?php
clearstatcache();
$dir = "EPM/";
if(isset($_GET['dir'])) { $dir = $_GET['dir']; }
$handle = opendir($dir);
if($handle == FALSE) { die("Error opening dir."); }
echo "Files in: " . $dir . "<br>";
while(false !== ($filename = readdir($handle))) {
if($filename == "." or $filename == "..") { echo $filename.'<br>';} else { 
if(is_dir($filename) ==  TRUE) { echo '<a href="?dir='.$dir.$filename.'/"> ->  '.$filename.'</a><br>'; } else {
echo '<a href="'.$dir.$filename.'">'.$filename.'</a><br>'; }}
}
closedir($handle);
?>

Link to comment
https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/
Share on other sites

Still wondering why my attempt #1 was skipping over some directories, or is that a mystery better off forgotten? :-\

I created this file-tree script for fun, its very interesting to do it in your the root directory.

File 1 = filelist.php

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Files</title>
<style type="text/css">
<!--
body li {
list-style-type: square;
list-style-position: outside;
text-indent: 10px;
}
-->
</style>
</head>

<body><div id="files">
<?php $dir = 'EPM/';
if(isset($_GET['dir'])) { $dir = $_GET['dir'] ; } ?>
<h3>Files in: <?php echo $dir; ?></h3>
<?php foreach (glob($dir."*", GLOB_MARK) as $filename) {
if(is_dir($filename)) { $tdir = $filename; echo '<a href="?dir='.$filename.'">>'.$filename.'</a><br>'; include('filelist2.php');} else {
echo '<li><a href="'.$filename.'">'.$filename.'</a></li>'; }}?>
</div>
</body>

File 2 = filelist2.php

<ul><?php foreach (glob($tdir."*", GLOB_MARK) as $filename) {
if(is_dir($filename)) { $tdir = $filename; echo '<a href="?dir='.$filename.'">'.$filename.'</a><br>'; include('filelist2.php');} else {
echo '<li><a href="'.$filename.'">'.$filename.'</a><br></li>'; }}?>></ul>

to see the root, simply have the url look like this .../filelist.php?dir=/

Also, you can change the defult folder by changing $dir =

I hit the stop button after about 30 seconds, little worried about what might happen.

Link to comment
https://forums.phpfreaks.com/topic/126351-is_dir-hiccup/#findComment-653452
Share on other sites

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.