I want to change the owner of a directory recursively using PHP. The chown
function works only on a single file.
I know I can use the below command from the terminal:
sudo chown -R user /path/to/dir/
How do I achieve the same in PHP?
Advertisement
Answer
/** * Recursively chown a directory * * @param string $dir * @param string|int $user */ function rchown($dir, $user) { $dir = rtrim($dir, "/"); if ($items = glob($dir . "/*")) { foreach ($items as $item) { if (is_dir($item)) { rchown($item, $user); } else { chown($item, $user); } } } chown($dir, $user); }