Folder permissions resetting to 0

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Folder permissions resetting to 0

Post by jayshields »

Yo.

I'm currently writing a script to upload images. I know I need to CHMOD the upload directory to 0755 before I try it or it won't work. So if I change the permissions to 0755 and use it, it works fine, and if I do another quite soon after it works also, but if i wait a while and try it again, it doesn't work (unable to open stream), and if I check the permissions of the folder they are set to 0.

The permissions keep resetting and I can't stop it from doing so!

I've even added

Code: Select all

chmod($uploaddir, 0755) or die('Couldn\'t change the persmissions on the upload directory.');
to my script just before the image upload snippet and it still doesn't work, and when I check the permissions on the folder afterwards, it is set to 0. I know chmod() can not work if it is not executed by the owner (or user with adequate priviledges), so I was hoping it would die on any instance of the permissions not being set.

Is this something to do with Apache or something else?

Thanks.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Is Safe Mode on?

You can probably use FTP to chmod() ... or just move the file by FTP and forget the chmod() all together.

Code: Select all

function annwyn_move_uploaded_file($source, $dest) {
    
    if(is_uploaded_file($source)) {
        $fp = fopen($source, 'rb');
        if($fp) {
            $conn_id = ftp_connect(FTP_UPLOAD_HOST);
            $login_result = @ftp_login($conn_id, FTP_UPLOAD_USER, FTP_UPLOAD_PASS);
            if($conn_id && $login_result) {
                $put_result = ftp_fput($conn_id, $dest, $fp, FTP_BINARY);
                fclose($fp);
                if($put_result) {
                    return true;
                }
            }
        }
    }
    
    return false;
    
}
Once again a work in progress ... :oops:

I setup a separate FTP user for this function and restricted the user to the upload directory.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Using the form to upload the image is the whole point :) If I used my FTP client to upload all the images I would've of bothered coding this, if that's what you mean...?

I don't think I should have to recode the file upload process, it's taked me literally forever to do it. I'm going to look into changing the permissions using some ftp commands that you've used to upload the file.

Edit: Seems ftp_chmod() does exist. Why would such a command exist when there's chmod() already? I'll try forcing permissions using ftp_chmod() later.

Thanks anyway mate.

Edit2: Ok, after messing with writing this FTP chmod code for ages now I get call to undefined function. Then realise you need to edit php.ini to enable the FTP function, but I'm on a shared host. So that's that.

I'm pretty sure safe mode isn't on.

I'm thinking maybe you just can't CHMOD directories with PHP or something...?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can't chmod a directory you don't own or have the rights to do so in.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Ok, I've worked it out.

For some absolutely absurd reason it wouldn't let me CHMOD the directory by referring to the path ('path/'), I say wouldn't, it did, because it never triggered my or die() statement. It just didn't work.

I had to write a recursive function to go through all the files in the directory CHMOD'ing them to my permission setting, even though the permission of the other files in the folder doesn't matter at all.

I only figured this out when I realised my PHP file manager that I got from somewhere could CHMOD directories just fine, so I got the idea when I looked into the code.

The function I used is here:

Code: Select all

function chmoddir ($dir, $perm) {
	$handle = opendir($dir);

	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			if (is_dir($dir.$file)) {
				chmoddir ($dir.$file.'/',$perm);
				chmod($dir.$file,$perm);
			} else {
				chmod($dir.$file,$perm);
			}
		}
	}
	closedir($handle);
}
Well that's weird to me. I hope someone else can make sense of how that solved it.
Post Reply