Facing “Permission Denied” errors? Use this simple PHP script to recursively change folder permissions via cPanel. Includes safe permission levels, examples, and troubleshooting tips.
<?php
// Change folder permissions recursively
function chmod_r($path, $perm) {
if (!file_exists($path)) {
echo "Path does not exist: $path";
return false;
}
// Apply permission
chmod($path, $perm);
if (is_dir($path)) {
$items = scandir($path);
foreach ($items as $item) {
if ($item !== '.' && $item !== '..') {
$fullPath = $path . '/' . $item;
chmod_r($fullPath, $perm);
}
}
}
return true;
}
// Example usage
$target_folder = '/home/username/public_html/yourfolder'; // change this
$permission = 0755; // or 0777
chmod_r($target_folder, $permission);
echo "Permissions updated successfully!";
?>
0 comments:
Post a Comment