PHP: Hypertext Preprocessor (original) (raw)

chown

(PHP 4, PHP 5, PHP 7, PHP 8)

chown — Changes file owner

Description

Parameters

filename

Path to the file.

user

A user name or number.

Return Values

Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.

Examples

Example #1 Simple chown() usage

<?php// File name and username to use $file_name= "foo.php"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>a</mi><mi>t</mi><mi>h</mi><mo>=</mo><mi mathvariant="normal">&quot;</mi><mi mathvariant="normal">/</mi><mi>h</mi><mi>o</mi><mi>m</mi><mi>e</mi><mi mathvariant="normal">/</mi><mi>s</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>s</mi><mi mathvariant="normal">/</mi><mi>p</mi><mi>h</mi><mi>p</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>e</mi><mi>t</mi><mi mathvariant="normal">/</mi><mi>p</mi><mi>u</mi><mi>b</mi><mi>l</mi><mi>i</mi><msub><mi>c</mi><mi>h</mi></msub><mi>t</mi><mi>m</mi><mi>l</mi><mi mathvariant="normal">/</mi><mi>s</mi><mi>a</mi><mi>n</mi><mi>d</mi><mi>b</mi><mi>o</mi><mi>x</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">&quot;</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">path = &quot;/home/sites/php.net/public_html/sandbox/&quot; . </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">&quot;/</span><span class="mord mathnormal">h</span><span class="mord mathnormal">o</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord">/</span><span class="mord mathnormal">s</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord mathnormal">es</span><span class="mord">/</span><span class="mord mathnormal">p</span><span class="mord mathnormal">h</span><span class="mord mathnormal">p</span><span class="mord">.</span><span class="mord mathnormal">n</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord">/</span><span class="mord mathnormal">p</span><span class="mord mathnormal">u</span><span class="mord mathnormal">b</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">h</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord">/</span><span class="mord mathnormal">s</span><span class="mord mathnormal">an</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord mathnormal">o</span><span class="mord mathnormal">x</span><span class="mord">/&quot;.</span></span></span></span>file_name ; $user_name = "root";// Set the user chown($path, $user_name);// Check the result <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">stat = stat(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>path); print_r(posix_getpwuid($stat['uid']));?>

The above example will output something similar to:

Array ( [name] => root [passwd] => x [uid] => 0 [gid] => 0 [gecos] => root [dir] => /root [shell] => /bin/bash )

Notes

Note: This function will not work onremote files as the file to be examined must be accessible via the server's filesystem.

Note: On Windows, this function fails silently when applied on a regular file.

Found A Problem?

martijn at sigterm dot nl

21 years ago

`If chown is filled with a variable ( chown ("myfile", $uid) the uid will be looked up through pwget_uid.

So if you need to set a non existing uid use inval($uid).

`

mindlessconsumer+phpnet at gmail dot com

3 months ago

`` It may be worth making explicitly clear that, while the shell's chown command allows both user and group to be set in one system call like this chown username:groupname filename, PHP's version unfortunately does not:

``