Jump to content

renaming folders only works with chmodd 777


mindspins

Recommended Posts

I'm using the following code in a WordPress functions file to rename a folder in the uploads folder according to the date, but this only works if the chmodd settings of both folders are 777. How can I change this code so I can use at least chmodd 755 to keep my website save.

 

date_default_timezone_set('Europe/Amsterdam'); // set timezone
$foldername = '';
$source = 'uploads';
$allfolders = getDirectory($source); // get folders
$folder = $source.'/'.$allfolders[0]; // get first folder
$month = (int)date('n');
$day = (int)date('j');
$f = 50-$day; $s = 100-$month;
$newfolder = $source.'/'.(string)$f.(string)$s;
if($folder != '' && $folder != $newfolder){
rename($folder, $newfolder);
$foldername = $newfolder."/";
}else if($folder == $newfolder){
$foldername = $newfolder."/";
}
else{ $foldername = 'error';
}

Steps for you:

1. Move everything in the uploads folder someplace else temporarily

2. Delete the uploads folder

3. chmod 077 the folder which contained the uploads folder

 

In a PHP script:

4. Recreate the uploads folder. Keep the default 0755 permissions

5. Move everything back

 

You:

6. chmod 0755 the parent folder

 

This way PHP is the owner of the uploads folder and can do whatever it wants inside of it. Alternatively, if you have root access, you just chown the folder to whatever user PHP/Apache runs as.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.