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

Build Your Private AI like ChatGpt with Ollama: A Step by Step Guide

Have you ever wanted to create your own private AI, like ChatGPT, to ensure and keep your chat with AI private and secure? With new advancements in AI technology, it is now possible to create and run AI models locally on your machine/desktop. In this article, we will...

Virtualization: Revolutionizing Computing Infrastructure

Virtualization is a transformative concept in computing that involves simulating hardware and software, creating a virtual environment to run multiple applications and operating systems on a single physical server. This paradigm shift from the traditional...