[SOLVED] Reading directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

[SOLVED] Reading directories

Post by $var »

hello,

for an update page, i would like to open OR make the image directory if it doesn't exist.
i started with this:

Code: Select all

$dir_handle = @opendir($path) or die("Unable to open $path");
	while ($file = readdir($dir_handle))
         { 
		if($file != "." && $file != "..") 
		{
			$imagearray[] = $file; 
		}

	} 
	closedir($dir_handle);
	$numimages=0;
	if($imagearray)
	{
		sort($imagearray);
		$numimages = count($imagearray);
	}
and it would work if the directory exists. if it didn't, the error message would show on a white page.
i added the mkdir to this and the directory was created.

Code: Select all

$dir_handle = @opendir($path) ||  @mkdir($path) or die("Unable to open $path");
	while ($file = readdir($dir_handle))
        { 
		if($file != "." && $file != "..") 
		{
			$imagearray[] = $file; 
		}

	} 
	closedir($dir_handle);
	$numimages=0;
	if($imagearray)
	{
		sort($imagearray);
		$numimages = count($imagearray);
	}
now, i get this error, and it doesn't read the directory (which makes sense from the error):

Code: Select all

Warning: readdir(): supplied argument is not a valid Directory resource in /home/.ufowasherkiln/meaghanb/londonindie.com/admnband/bands/editband.php on line 26

Warning: closedir(): supplied argument is not a valid Directory resource in /home/.ufowasherkiln/meaghanb/londonindie.com/admnband/bands/editband.php on line 34


Does anyone have any idea why it's being fussy over the readdir and closedir?
Last edited by $var on Tue Apr 04, 2006 9:31 am, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

mkdir() does not return directory resources.

Furthermore, || the two commands will give you back a bool, not the return values of the functions. Just use the regular if()
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think you would be better off with some combination of is_dir() and file_exists() to do the check up front.
(#10850)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Code: Select all

if(!is_dir($path)) { //if $path isn't a dir
  mkdir($path); //create it
}

//now $path definately exists, do what you want with it
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

jay sheilds... thanks so much for that one.. i think it will be the best fit here
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

No problem, but remember, 'i' before 'e' except after 'c' ;) Oh, and before a smart ass butts in, I know there are a few exceptions of that phrase...!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Might want to specify the mode when using mkdir() for safety's sake.

Code: Select all

<?php mkdir('dir', 0744); ?>
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

are you referring to 0744 as the CHMOD settings? wow... you can do that eh? neat.
i manually set them as 777 for that folder.

Sorry Mr. Shields
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Yes, the mode setting (CHMOD = CHange MODe)

And 777 is a BAD idea.. that allows everyone to Read, Write and Excute to that directory.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

i can see the execute being a bad idea, but other than that... if i take those off will that be 744?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Mode works by 3 x 3 (though it is an octal number.. different story)

The first number relates to the Owner of the file/directory (the user which was used to create it)

The second number refers to the group that the user used to create it belongs to.

The third number relates to all else.

0 = none (deny Read, deny Write, deny Execute.) File is effectively non-existant (more spefically hidden, can't be seen, changed or run by the 'group' that this number is assigned to in the chmod setting)

1 = Execute only (deny Read, deny Write).

2 = Write only (deny Read, deny Execute.)

3 = Write and Execute (deny Read.)

4 = Read only (deny Write, deny Execute.)

5 = Read and Execute (deny Write.)

6 = Read and Write (deny Execute.)

7 = Read, Write and Execute.

So,

Code: Select all

<?php chmod('file', 0754); ?>
Will set the permissons for the current username to be able to read, write and execute the file.

The group of which username is a part of will be able to read and execute the file.

Other (Everyone else) can only read the file.

0755 is popular amongst php developers because if you are hosting the file publically, you want people to be able to see it, and to have it as part of a PHP script it must be executable. Yet the only person you want to allow to update it, is your own user.

In this case it will be lmitied to the user that php was started with, but most hosts run as CGI which means each user (you) gets their own username/id anyway.. but I digress...
Post Reply