Reset Permissions

Sometimes a host will reset your permissions.  Well it seems that's what happens, whether it's the host or someone else doesn't really matter, you go to make a change to the configuration and you find that it doesn't save and that's because the permissions are incorrect.

* Note * If you are on a Windows server you cannot change the permissions by FTP.  Most Windows hosts give you a method to set permissions through the control panel.  If you can't find it contact your host and ask for assistance.

To change file permissions on non windows servers login using FTP and set the permissions to 755 (or 777 see below) for the following folders and include all files and subdirectories:-

/data/
/templates/

In FileZilla this is quite easy, highlight the folders you want and select File Attributes .

Modify folder file attributes in FileZilla

You can then set the permissions to 755 and remember to select Recurse subdirectories and the Apply to all files and directories before clicking the OK button.

File attributes dialogue

What should I set my permissions to?

Should it be 644, 755 or 777?

There seems to be some office politics over the correct permissions to set your files.  Often hosting documentation treats it like a disease with vague references and suggestions but some files have to be writeable and some needed to be executed.  Perhaps the best solution is to proceed with caution at all times but recognise that some risks are required and those files that are vulnerable should be backed up before and after changes are made.

Some hosts will tell you that you do not need more than 755 tow write to files, and that is correct... for some hosts.  Yet others cannot be written to if the execution permission is also granted.  It depends on the setup.  If you wish to be extra cautious try 755 first and if everything works then know you are a little better off than someone who needs 777... but not much!

Remember the three most important things when it comes to your files: backup backup and backup!

Is there an easier way to do all this?

We have written a script that will probably solve the problem for you automatically if you are on a Linux or Unix machine.  If you are on Windows and have no way to set your file permissions you must ask your host.

Copy the following code into a text file and name it setperms.php.  Then upload to the root folder of your web and run it through the browser.  All you have to do is provide your FTP details and the script will take care of everything for you.  Remember to delete the file when you are done.

<html>
<body>

<h1 align="center">Preparing standard permissions (v1.41)</h1><br>
<b>Description</b><br>
This script will set the default permissions for a
<a href="http://www.ventrino.com" target="_blank">Ventrino
Traffic Exchange Script</a>.  Save this script to check your
file permissions regularly.<br><br>Server Root: <?php echo $_SERVER['DOCUMENT_ROOT']; ?>
<br><br>

<form method="post" action="setperms.php">
<table border="0">
<tr><td>FTP Username:</td><td><input type="text" name="u"></td></tr>
<tr><td>FTP Password:</td><td><input type="password" name="p"></td></tr>
<tr><td> </td><td><input type="submit" value="Submit"></td></tr>
</table>
<form>


<?php
/*****************************************************************
 * Setting file permissions for a walker/ventrino script using FTP
 *  
 * IMPORTANT PROGRAMMING NOTE
 * The FTP logins in and sees the directory structure from the
 * hosts root directory.  Typically this looks something like
 * /home/username/ whereas the script is running from the web
 * folder something like /home/username/public_html.
 * BE WARNED that this script searches for files to chmod using
 * the public_folder at it's starting point and this need to be
 * translated to the folder structure seen by the FTP!    
 *****************************************************************
*/



function chmod_open($user, $pass)
{
    $ftp_user_name = $user;
    $ftp_user_pass = $pass;
    $ftp_server = 'localhost';
    $ftp_root = '/';
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    echo "<br>Login result: $login_result<br>";
    return $conn_id;
}

function chmod_file($conn_id, $permissions, $path) {
  if (ftp_site($conn_id, 'CHMOD ' . $permissions . ' ' . $ftp_root . $path) !== false){
    return TRUE;
  } else {
    return FALSE;
  }
}

function chmod_close($conn_id) {
  ftp_close($conn_id);
}

function htmlpath($relative_path) {
    $rootpath = strrev($_SERVER['DOCUMENT_ROOT']);
    $startpos = strpos($rootpath,'/');
    $rootpath = substr($rootpath,0,$startpos);
    $rootpath = strrev($rootpath);
   
    if (substr($relative_path,0,1) == "/") {
      return $rootpath.$relative_path;
    } else {
      return $rootpath."/".$relative_path;
    }
}

function recchmod($conn_id,$filemode,$path) {
  echo chmod_file($conn_id, $filemode, htmlpath($path)) ? "<br>chmod ".htmlpath($path)." success..." : "<br><b>chmod ".htmlpath($path)." error</b>";
  $dh = opendir($path);
  while ($file = readdir($dh)) {
      if($file != '.' && $file != '..') {
          $fullpath = dirname(__FILE__).'/'.$path.'/'.$file;
          $chmodpath = htmlpath($path.'/'.$file);
          if(!is_dir($fullpath)) {
            echo chmod_file($conn_id, $filemode, $chmodpath) ? "<br>chmod $chmodpath success..." : "<br><b>chmod $chmodpath error</b>";
          } else {
            echo '<br><b>Folder: '.$fullpath.'</b>';
            $newpath = $path.'/'.$file;          
            recchmod($conn_id, $filemode, $newpath);
          }
      }
  }
  closedir($dh);
}

function chmod_path($conn_id,$path) {
  if (file_exists($path)) {
    echo "<br>$path found, setting permissions...";
    recchmod($conn_id,'777',$path);
  } else {
    echo "<br>$path not found";
  }
}

if (($_POST['u'] != "") && ($_POST['p'] != "")) {
  echo '<br>setting permissions...';
  $conn_id = chmod_open(htmlspecialchars($_POST['u']),htmlspecialchars($_POST['p']));

  //chmod individual files if they exist
  $infile = 'mysql_config.php';
  if (file_exists($infile)) {
    $infile = htmlpath('mysql_config.php');
    echo "<br>$infile found, setting permissions...";
    echo chmod_file($conn_id, '777', $infile) ? "<br>chmod ".$infile." success..." : "<br><b>chmod ".$infile." error</b>";
  } else {
    echo "<br>$infile not found";
  }

  //Now do sub directories    
  chmod_path($conn_id,'data');
  chmod_path($conn_id,'templates');
  chmod_path($conn_id,'downloads');

  chmod_close($conn_id);
}


?>
</body>
</html>
 

 

 

 

Home Forum Client Area