Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > PHP's is_dir doesn't work properly in Tiger?

PHP's is_dir doesn't work properly in Tiger?
Thread Tools
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
May 18, 2005, 08:50 AM
 
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
Reply With Quote
May 18, 2005, 11:17 AM
 
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
Reply With Quote
Jul 7, 2005, 09:34 AM
 
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
Reply With Quote
Jul 20, 2005, 10:00 PM
 
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
Reply With Quote
Jul 21, 2005, 04:15 AM
 
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
Reply With Quote
Jul 21, 2005, 09:30 AM
 
How are you going to do that without PHP?
     
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Jul 21, 2005, 09:49 AM
 
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
Reply With Quote
Jul 21, 2005, 01:16 PM
 
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
Reply With Quote
Jul 21, 2005, 03:10 PM
 
try the following:

if( is_dir( "events/" . $file))
     
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Jul 23, 2005, 04:55 AM
 
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
Reply With Quote
Jul 23, 2005, 11:42 AM
 
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
Reply With Quote
Jul 23, 2005, 01:03 PM
 
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
Reply With Quote
Jul 24, 2005, 08:56 AM
 
I said "practical."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:14 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2