chmod to rw code example

Example 1: chmod

sudo chmod -R 777 name-file-wht-upu-want     ///open for accsess all users 
sudo chmod 644  #for example file /etc/hosts 
https://ss64.com/bash/chmod.html

Example 2: running chmod command using code

private function chmod_r($dir, $permission)
{
  $dp = opendir($dir);

  while($file = readdir($dp))
  {
    if (($file == ".") || ($file == "..")) continue;

    $path = $dir . DIRECTORY_SEPARATOR . $file;
    $is_dir = is_dir($path);

    $this->set_perms($path, $is_dir, $permission);
    if($is_dir) {
    	$this->chmod_r($path, $permission);
    }
  }
  closedir($dp);
}

private function set_perms($file, $is_dir, $permission)
{
  $perm = substr(sprintf("%o", fileperms($file)), -4);
  $dirPermissions = $permission;
  $filePermissions = $permission;

  if($is_dir && $perm != $dirPermissions){
  	chmod($file, octdec($dirPermissions));
  }
  else if(!$is_dir && $perm != $filePermissions){
  chmod($file, octdec($filePermissions));
  }

  flush();
}

$permission = '0777';

$dir = storage_path('framework');

$this->chmod_r($dir, $permission);