PHP/File Directory/chmod

Материал из Web эксперт
Перейти к: навигация, поиск

Changing file permissions

 
<?php
chmod("/home/user/secrets.txt",0400);
?>



Changing File Permissions and Ownership

 
<?
    chmod("/var/www/myfile.txt", 0777);
    chmod("/var/www/myfile.txt", 0755);
    if (chown("myfile.txt", "sally")) {
            print "File owner changed.\n";
    } else {
            print "File ownership change failed!\n";
    }
?>



chmod() function changes the mode of filename to permissions.

 
Its syntax is: int chmod (string filename, int permissions)
The permissions must be specified in octal mode. 
<?
    chmod ("data.txt", 0766); // This will work
?>



Using the chmod() Function

 
<?php
     $fr = @fopen("data.txt", "w");
     if(!$fr) {
          chmod("data.txt", 0722);
          $fr = @fopen("data.txt", "w");
          if(!$fr) {
          echo "Error: Couldn"t open data.txt (chmod attempted)";
               exit;
          }
     }
     fputs($fr, "Write Successful!");
     fclose($fr);
?>