views:

690

answers:

3

Hi there,

I am trying to write an install script for a system I have been working on. The script copies some default files from one location to another, and creates various folders for them. I have this bit working a treat, but the only problem is that when I login via FTP, I can't edit, or delete the files that PHP has moved for me.

If I login via terminal I can happily "sudo chmod -R 777 [dir]" and the problem goes away, so the question is:

What am I missing on the PHP end?

my permissions function is as follows:

function set_permissions($file) 
{
  if (file_exists($file)):
    chmod($file,0777);  
  endif;       
}

I understand it's not 100% ideal to set the permissions to 777, but I am simply trying to achieve the result of being able to edit the files via FTP, after PHP has moved them for me.

Hope I've been clear enough. This is puzzling me now so any help is appreciated :)

Tom

edit: The whole process is as follows:

mkdir($root_dir, 0777);
mkdir($images_dir, 0777);
if (!copy($orig_logo, $new_logo)) 
{
  echo "failed to copy $orig_logo...";
} 
  // see function above for details on set_permissions...    
  $this->set_permissions($new_logo);
}

(All the paths are correct too)

edit: The file before I login via terminal has the following permissions:

-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.css

The file after I login and change it has:

-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.css

The system is a content management system that allows you to edit and delete files through the admin area, and strangely enough, this works well. It seems like the files are somehow locked out from anybody else other than Apache... but the file info suggests otherwise. It's odd...

+1  A: 

Are you sure the file exists or the path is correct?

RioTera
+1  A: 

Sounds like your directory needs the write permissions as well.

Nerdling
sorry I should have mentioned that the directories that I am moving the files also have permissions applied to them. Before I move the files to the directories, I make the directories using the following: mkdir($default_dir, 0777);Hope this helps...
Tisch
No ACLs in effect?
Nerdling
sorry for being thick? ACL's?
Tisch
access control lists: http://en.wikipedia.org/wiki/Access_control_list
Nerdling
+1  A: 

If you can chmod -R 777 via terminal to fix the problem, then what were the permissions set to by PHP before you ran chmod??? Obviously not 777. My guess is that your PHP code is not actually changing the permissions.

Lookat at your code, your permission-changing function could be failing silently if the file doesn't exist - e.g., you're giving it invalid file names (wrong folder? wrong relative path?) but you can't tell because your set_permissions() function is too scared of warning you. You should rewrite it as follows:

function set_permissions($file) 
{
  if (!file_exists($file))
    throw new Exception(__FUNCTION__ . "() file doesn't exist! '$file'");
  chmod($file,0777);
  error_log("chmod 777 $file"); // debug
}

This allows you to see what's happening, and you'll certainly notice if you haven't got your file names correct.

too much php
The file before I login via terminal has the following permissions:-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.cssThe file after I login and change it has: -rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.cssSo in response, I have to confirm that the PHP is running correctly. It's also worth noting that if I use my system to detail the files via a browser... it will happily delete them, and I can also edit them using my system too.
Tisch