Fix Permissions of files php

Fix & Change Folder / Files Permissions in easy way with PHP

If you are looking for code that can fix files and folder permissions in just one click here is the code to do it. Once you save it in a .php file and run it will run automatically locate the folder or directories and scan them after it will apply the give permissions to files and folders.

To use it copy the code below create a file with .php extension example ” fix.php in public_html folder or main directory of your website and open it with code editor by right click on it and paste the code and save it.

Then open the file in browser like: lvato.com/fix.php . Once you will open it in browser it will scan the folders and files and fix the permission by changing which you applied.

<?php

function chmod_r($dir, $dirPermissions, $filePermissions) {
  $dp = opendir($dir);
  while($file = readdir($dp)) {
    if (($file == ".") || ($file == "..")) continue;
    $fullPath = $dir. "/". $file;
    if(is_dir($fullPath)) {
      chmod($fullPath, $dirPermissions);
      chmod_r($fullPath, $dirPermissions, $filePermissions);
    } else {
      chmod($fullPath, $filePermissions);
    }
  }
  closedir($dp);
}

$startDir = dirname(__FILE__);
chmod_r($startDir, 0755, 0655);

echo "All folders and files in the directory have been found and their permissions have been set.";

?>

Related Articles

Handling a 50,000 Users Cron Job in PHP Without Breaking the System

When a system grows to tens of thousands of users, the real challenge is no longer features—it’s how efficiently you process data repeatedly without collapsing your database or server. A common mistake in PHP applications is treating cron jobs as “run everything every...

How to Check If Your Next SaaS Project Can Handle High Traffic

It is not features that usually kill most SaaS applications; they die when traffic hits them. All good until it starts going live… and then the site slows down, the API gets slow, and all sorts of issues related to databases show up, making that "scalable SaaS" look...