Advertisement
Tuurlijk

Chmod Test

Jan 13th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Here's a little test to figure out how PHP handles this whole chmod
  4.  * situation. We try both string values and plain int values to set the
  5.  * access controls.
  6.  *
  7.  * It turns out that mkdir does not respect the sticky bit. An extra chmod is
  8.  * needed to add it. Not even ignoring the umask helps. Is this a PHP bug?
  9.  */
  10. $testPath = '/Users/michiel/wb/WebRoot/6.2.master.workbench.local/fileadmin/test/';
  11.  
  12. $tests = array(
  13.     'string' => array(
  14.         'file' => octdec('0664'),
  15.         'directory' => octdec('02775')
  16.     ),
  17.     'integer' => array(
  18.         'file' => 0664,
  19.         'directory' => 02775
  20.     )
  21. );
  22.  
  23. $expected = array(
  24.     'file' => '100664',
  25.     'directory' => '42775'
  26. );
  27.  
  28. var_dump($tests);
  29.  
  30. foreach ($tests as $type => $testData) {
  31.     echo '<h3>' . $type . ' Mode test</h3><br/>';
  32.     foreach ($testData as $nodeType => $mode) {
  33.         echo 'Mode test using ' . $nodeType . '. Outcome should be ' . $expected[$nodeType] . '<br/>';
  34.         if ($nodeType === 'file') {
  35.             echo 'Plain chmod.<br/>';
  36.             $nodename = $testPath . $type . '-' . $nodeType . '-plain.txt';
  37.             file_put_contents($nodename, 'lala');
  38.             chmod($nodename, $mode);
  39.             var_dump(decoct(fileperms($nodename)));
  40.         } else {
  41.             echo 'Plain mkdir.<br/>';
  42.             $nodename = $testPath . $type . '-' . $nodeType . '-plain';
  43.             mkdir($nodename, $mode);
  44.             var_dump(decoct(fileperms($nodename)));
  45.  
  46.             echo 'Using mkdir and extra chmod.<br/>';
  47.             $nodename = $testPath . $type . '-' . $nodeType . '-extra-chmod';
  48.             mkdir($nodename, $mode);
  49.             chmod($nodename, $mode);
  50.             var_dump(decoct(fileperms($nodename)));
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement