 |
 |
PHP's is_dir doesn't work properly in Tiger?
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
I just want to automate certain steps in creating my webpage, so I wanted to learn php in the process (I could do it with the command line).
So I whipped up a simple
Code:
<?php
$curr_dir=opendir('./events');
while ($file=readdir($curr_dir)) {
if($file != "." && $file != "..")
if(is_dir($file))
echo("$file ");
}
closedir($curr_dir);
?>
But it doesn't give me anything in a directory which is full of subdirectories. The only thing it would list are . and .. which I explicitly excluded for obvious reasons. When I choose another directory, the directory it contains (only one) is listed properly. What's wrong? Sorry for such a newbie question … or is this really a bug?
|
|
I don't suffer from insanity, I enjoy every minute of it.
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
I came a bit closer to the solution. If I copy the script in the `events' folder and modify the script to opendir('.') everything works as expected.
What am I missing here?
Oh, and is_file as well as a simple directory contents list works as expected in both cases.
|
|
I don't suffer from insanity, I enjoy every minute of it.
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I've hit this is_dir bug too. PHP in Tiger seems to think everything is not a directory. I don't know what's going on.
Edit: Nevermind, I was passing bad paths to it. I think it's working right for me now.
(Last edited by wataru; Jul 12, 2005 at 08:04 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 1999
Status:
Offline
|
|
Not really sure what you are trying to do...but this works:
<?PHP
$maindir=".";
$mydir=opendir($maindir);
$exclude_array=array(".","..",".DS_Store");
while (false !== ($fn=readdir($mydir))) {
if ($fn==$exclude_array[0] || $fn==$exclude_array[1] || $fn==$exclude_array[2]) continue;
$fn2=substr("$fn",0);
echo "<br><a href='".rawurlencode ($fn)."'>$fn2</a>";
}
closedir ($mydir);
?>
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
Originally Posted by ppmax
Not really sure what you are trying to do...but this works:
<?PHP
$maindir=".";
$mydir=opendir($maindir);
$exclude_array=array(".","..",".DS_Store");
while (false !== ($fn=readdir($mydir))) {
if ($fn==$exclude_array[0] || $fn==$exclude_array[1] || $fn==$exclude_array[2]) continue;
$fn2=substr("$fn",0);
echo "<br><a href='".rawurlencode ($fn)."'>$fn2</a>";
}
closedir ($mydir);
?>
My provider doesn't support php, but because my website is very complex already, I want to automate as much as I can. Keeping the menus in sync in particular is a bitch. So I want to write a script that lists all directories and reads a description file from each one. The it puts it all together to make the necessary menu files.
|
|
I don't suffer from insanity, I enjoy every minute of it.
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
How are you going to do that without PHP? 
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
With a shell script for instance. But it's a lot slower and less comfortable to work with.
|
|
I don't suffer from insanity, I enjoy every minute of it.
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
You mean just locally? That's easy with bash.
Code:
MYDIR=$HOME/Sites/blah
for ITEM in $(ls $MYDIR); do
if [[ -d $ITEM ]]; then
cat $MYDIR/$ITEM/properties.html
# or, if you want the file to contain variables like NAME="My Page"
# . $MYDIR/$ITEM/properties.sh
# then deal with your variables
fi
done
(Last edited by wataru; Jul 21, 2005 at 01:26 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jan 2000
Location: Burlington, VT, USA
Status:
Offline
|
|
try the following:
if( is_dir( "events/" . $file))
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
I know how to do with with a shell script, I just wanted to learn something new, php 
|
|
I don't suffer from insanity, I enjoy every minute of it.
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
PHP is much uglier for reading directories. You have to open the directory and set it to a handle, then loop through the directory to get the names of each item. Here's an example:
Code:
if($dir = opendir($path)) {
while (($entry = readdir($dir)) != false) {
// do stuff
}
} else {
return "Error: Couldn't open directory";
}
If there's an easier way I'd love to hear it.
Of course if this is strictly local stuff there's no practical reason to do it in PHP.
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: May 2001
Location: Hilbert space
Status:
Offline
|
|
Originally Posted by wataru
PHP is much uglier for reading directories. You have to open the directory and set it to a handle, then loop through the directory to get the names of each item. Here's an example:
Code:
if($dir = opendir($path)) {
while (($entry = readdir($dir)) != false) {
// do stuff
}
} else {
return "Error: Couldn't open directory";
}
If there's an easier way I'd love to hear it.
Of course if this is strictly local stuff there's no practical reason to do it in PHP.
I'll try that tonight or tomorrow. There's a good reason to do it: it's fun to learn new things. And also, one day, my web host will support it and then things are good again.
|
|
I don't suffer from insanity, I enjoy every minute of it.
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I said "practical." 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|