PDA

View Full Version : Resolved opendir?



bluewalrus
12-25-2009, 10:42 PM
This is only echoing one of my directories as a link. It's echos them all before the "if (is_dir)" and after the while (so between line 3 echo $file; would be). The only directory that echos is the only one that has contents, so does the directory have to have contents for the is_dir to be true? Thanks.



<?php
if ($handle = opendir('./images')) {
while (false !== ($file = readdir($handle))) {
if (is_dir($file)) {
if (substr("$file", 0, 1) != ".") {
?>
<a href="<?php echo $file; ?>"><?php echo $file; ?></a><br />
<?php
}
}
$value++;
}
closedir($handle);
}
?>

techietim
12-25-2009, 11:22 PM
This is probability a better solution for what you're trying to do, just make sure you add in the file extensions you wish to display:


$files = glob('./images/*.{jpg,png,gif}', GLOB_BRACE);

$value = 0;

foreach($files as $file)
{
echo '<a href="' . $file . '">' . $file . '</a><br />';
$value++;
}

bluewalrus
12-26-2009, 06:42 AM
I don't want to display any files only directories.

djr33
12-26-2009, 06:54 AM
This line is causing the problems:

if (substr("$file", 0, 1) != ".") {

1. Why did you use quotes? It should be $file, not "$file"

2. This says "if not directory", which is a contradiction to "if is_dir()".
"." is the first character of EVERY directory. "." is the current directory and ".." is one directory "up" in the filesystem. But other directories have a dot first too, like the directory "example" within the current directory is actually seen as ".example" when searching like this.

bluewalrus
12-26-2009, 07:10 AM
It use to be text never took the time to change when changed to variable, it still parses the same though right? That pulls out the .. and . from the listings of the directories, as well as any files that's first character is a . because they are suppose to be hidden.

Why does it say "if not directory" shouldn't/doesn't it say


if ($handle = opendir('./images')) { //open images folder
while (false !== ($file = readdir($handle))) { //read images folder
if (is_dir($file)) { //if file is a directory
if (substr("$file", 0, 1) != ".") { //if it's first character is not a "." ....next line echos a link to it because it is a directory.

This all works I get the directory to display and it hides all other files, but it only displays 1 of 8 directories the other 7 directories though do not have files in them so does the is_dir require the directory have contents? Thanks.

djr33
12-26-2009, 07:13 AM
Isn't the "." for all directories? Maybe I'm just very wrong then. If it's hidden files, sorry for a confusing answer. I meant that searching for a first character dot was searching for directories.

But again, why are you skipping the dot ones? I would use this line instead of that:
if ($file!='.'&&$file!='..') {

That should accomplish the same thing. See if that helps.

bluewalrus
12-26-2009, 07:16 AM
Still the same result, only the 1 directory listed.
I'm not sure on the "." and ".." I forget what they call back to right now.

That line wouldn't do the same though for all instances of directory reading, for example ".hidethisimage.jpg" would be displayed by that line. The . at the start of a file name on macs hides them not sure about windows or on the server if that is true without the line used above.

djr33
12-26-2009, 07:31 AM
Yes, I understand. I'm not exactly sure, then.

Nothing jumps out at me in your code. However, try changing this line:
if ($handle = opendir('./images')) {

'./images' is the same thing as just 'images'

I can't see anything else at the moment. If that doesn't work, I'll try to just rewrite it all tomorrow. It's not that much code :)

bluewalrus
12-26-2009, 02:29 PM
I forgot about that value++ variable that was to debug.

So this is my code now:


<?php
if ($handle = opendir('./images')) {
while (false !== ($file = readdir($handle))) {
if (is_dir($file)) {
if (substr($file, 0, 1) != ".") {
?>
<a href="<?php echo $file; ?>"><?php echo $file; ?></a><br />
<?php
}
}
}
closedir($handle);
}

This is the output from this:

<a href="fraser">fraser</a><br />

My debug code is:



<?php
$value = 0;
if ($handle = opendir('./images')) {
while (false !== ($file = readdir($handle))) {
echo "#" . $value . "&nbsp;" . $file . "<br />\n";
if (is_dir($file)) {
if (substr($file, 0, 1) != ".") {
?>
<a href="<?php echo $file; ?>"><?php echo $file; ?></a><br />
<?php
}
}
$value++;
}
closedir($handle);
}
?>


and the output is


#0&nbsp;fraser<br />

<a href="fraser">fraser</a><br />
#1&nbsp;icons<br />
#2&nbsp;hogan<br />

#3&nbsp;admin<br />
#4&nbsp;..<br />
#5&nbsp;.<br />
#6&nbsp;original<br />
#7&nbsp;loading.gif<br />
#8&nbsp;chris<br />


I also tried adding a file into the hogan directory but that didn't get it to show up so it's not that the direcotry needs content.

jscheuer1
12-26-2009, 05:07 PM
I figured it out. You need to supply the path to the is_dir() method, example:


<?php
$pth = './images/';
if ($handle = opendir($pth)) {
while (false !== ($file = readdir($handle))) {
if (is_dir($pth . $file)) {
if (substr($file, 0, 1) !== ".") {
?>
<a href="<?php echo $pth . $file; ?>"><?php echo $file; ?></a><br />
<?php
}
}
}
closedir($handle);
}
?>

Without that, the only directories that will pass is_dir() (be included in the list) are ones that are directories of both the root folder and of the images folder. So if you had a folder named 'test' in the root, and one named 'test' in the images folder, that one would show up.

Also note that if you do not include the path in the href (as shown above, href="<?php echo $pth . $file; ?>"), It will either be to the wrong folder, or to no folder.

It's not required here:


if (substr($file, 0, 1) !== ".") {

In fact, including it there would mess up that test.

Note: The root folder in the above is just where I was running this from. It could be any immediate parent folder of the (in this case images) folder being checked.

bluewalrus
12-26-2009, 09:12 PM
Oh, that makes a lot more sense now thanks. I didn't even think of the path in that conditional being the problem.

djr33
12-26-2009, 10:23 PM
Yes, that's right. I missed it last night.
All functions like is_dir(), etc., operate based on the current directory so once you are inside a folder (like images) you need to operate based on that-- add the path.
This usually becomes an issue with recursive functions, so I'm used to putting it there: you open a directory and run through the files then run through inside it's directories, and of course you'll need to start with the relative path.
But here is was just one folder, so it is easy to miss that-- except that you were in the first place operating in a different directory. readdir() is always just the filename, not path info.