Advertisement
willysec_id

Mass Set File and Directory Permissions

Jul 11th, 2024
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | Cybersecurity | 0 0
  1. <!-- This tools usefull when terminal or cmd function is disabled -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Bulk Set Directory Permissions</title>
  8. </head>
  9. <body>
  10.     <h1>Mass Set File and Directory Permissions</h1>
  11.     <form method="post" action="">
  12.         <label for="directory">Master Path:</label>
  13.         <input type="text" id="directory" name="directory" placeholder="/home/willygoid/public_html" required>
  14.         <label for="permission">Permission:</label>
  15.         <input type="number" id="permission" name="permission" value="0777" required>
  16.         <button type="submit">Set Permissions</button>
  17.     </form>
  18. </body>
  19. </html>
  20.  
  21. <?php
  22.     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  23.         $directory = $_POST['directory'];
  24.         $permission = $_POST['permission'];
  25.  
  26.         function setPermissions($dir) {
  27.             if (!is_dir($dir)) {
  28.                 throw new Exception("The provided path is not a directory");
  29.             }
  30.  
  31.             $iterator = new RecursiveIteratorIterator(
  32.                 new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
  33.                 RecursiveIteratorIterator::SELF_FIRST
  34.             );
  35.  
  36.             foreach ($iterator as $item) {
  37.                 if ($item->isDir()) {
  38.                     chmod($item, $permission);
  39.                 } else {
  40.                     chmod($item, $permission);
  41.                 }
  42.             }
  43.  
  44.             // Finally, set the top-level directory permissions
  45.             chmod($dir, $permission);
  46.         }
  47.  
  48.         try {
  49.             setPermissions($directory);
  50.             echo "<p>Permissions set to $permission for all files and directories in $directory</p>";
  51.         } catch (Exception $e) {
  52.             echo "<p>Error: " . $e->getMessage() . "</p>";
  53.         }
  54.     }
  55.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement