nimrod
nimrod

Reputation: 5732

Keep file permission and owner when copying file with PHP

How can I keep the file permission and the owner when I copy a file with PHP?

Code that copies file:

$source = CSS . 'customers' . DS . 'source.css';
$destination = CSS . 'customers' . DS . 'destination.css';

if(!@copy($source, $destination)) {
    $errors= error_get_last();
    echo "COPY ERROR: ".$errors['type'];
    echo "<br />\n".$errors['message'];
}

Output:

-rwxrwxrwx    1 myuser  admin  7145 11 Nov 20:02 source.css
-rw-r--r--    1 daemon  admin  7145 19 Nov 16:27 destination.css

Upvotes: 0

Views: 2164

Answers (1)

Hasse Bj&#246;rk
Hasse Bj&#246;rk

Reputation: 1601

Only admin/root can change owner of a file.

  • Apache is run as user apache or daemon, and can not change ownership.
  • CLI program run as user can not change owner
  • CLI program run as root can change owner

What you can do is set the file permissions to be readable by group or all users.

exec( 'chmod 664 ' . $filename );  // Group only
exec( 'chmod 666 ' . $filename );  // All users

Filename is the absolute filename with path or relative to scripts path!

Upvotes: 1

Related Questions