Added two examples for displaying permissions

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@163292 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Aidan Lister 2004-07-14 10:42:29 +00:00
parent 4c11703bfb
commit a9ab1c9404

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<!-- splitted from ./en/functions/filesystem.xml, last change in rev 1.2 -->
<refentry id="function.fileperms">
<refnamediv>
@ -15,11 +15,86 @@
<para>
Returns the permissions on the file, or &false; in case of an error.
</para>
&note.clearstatcache;
&tip.fopen-wrapper.stat;
<example>
<title>Display as an octal value</title>
<programlisting role="php">
<![CDATA[
<?php
// Returns 1777
echo substr(sprintf('%o', fileperms('/tmp')), -4);
// Returns 0644
echo substr(sprintf('%o', fileperms('/etc/passwd')), -4);
?>
]]>
</programlisting>
</example>
<example>
<title>Display permissions</title>
<programlisting role="php">
<![CDATA[
<?php
$perms = fileperms('/etc/passwd');
if (($perms & 0xC000) == 0xC000) {
// Socket
$info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
// Symbolic Link
$info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
// Regular
$info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
// Block special
$info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
// Directory
$info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
// Character special
$info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
// FIFO pipe
$info = 'p';
} else {
// Unknown
$info = 'u';
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
echo $info;
?>
]]>
</programlisting>
<screen>
<![CDATA[
-r--r--r--
]]>
</screen>
</example>
<simpara>
See also <function>is_readable</function>,
and <function>stat</function>
@ -27,6 +102,27 @@
</refsect1>
</refentry>
<!--
<?php
?>
-->
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml