Page 1 of 1

[SOLVED] Reading directories

Posted: Sun Jan 29, 2006 7:02 pm
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?

Posted: Sun Jan 29, 2006 7:57 pm
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()

Posted: Sun Jan 29, 2006 10:52 pm
by Christopher
I think you would be better off with some combination of is_dir() and file_exists() to do the check up front.

Posted: Mon Jan 30, 2006 5:31 am
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

Posted: Tue Jan 31, 2006 8:19 am
by $var
jay sheilds... thanks so much for that one.. i think it will be the best fit here

Posted: Tue Jan 31, 2006 8:35 am
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...!

Posted: Tue Jan 31, 2006 9:11 am
by Jenk
Might want to specify the mode when using mkdir() for safety's sake.

Code: Select all

<?php mkdir('dir', 0744); ?>

Posted: Tue Jan 31, 2006 9:21 am
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

Posted: Tue Jan 31, 2006 9:25 am
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.

Posted: Tue Jan 31, 2006 9:29 am
by $var
i can see the execute being a bad idea, but other than that... if i take those off will that be 744?

Posted: Tue Jan 31, 2006 9:45 am
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...