Home  >  Article  >  Backend Development  >  How to change file owner in php

How to change file owner in php

青灯夜游
青灯夜游Original
2021-07-14 19:03:543097browse

The chown() function can be used in php to modify the owner of the file. This function can change the owner of the specified file. If the modification is successful, it returns TRUE, if it fails, it returns FALSE; the syntax format is "chown(file ,owner)", the parameter owner specifies the new owner, which can be the user name or user ID.

How to change file owner in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use chown () function to modify the owner of the file.

chown — Change the owner of a file, returning TRUE on success or FALSE on failure.

Syntax

chown(file,owner)
Parameters Description
file Required. Specifies the documents to be checked.
owner Required. Provide new owner. Can be a username or the user's ID.

Example: Change file owner and user group

<?php
chown(&#39;/tmp/myfile.txt&#39;,&#39;sklar&#39;);           // specify user by name
chgrp(&#39;/home/sklar/schedule.txt&#39;,&#39;soccer&#39;); // specify group by name

chown(&#39;/tmp/myfile.txt&#39;,5001);              // specify user by uid
chgrp(&#39;/home/sklar/schedule.txt&#39;,102);      // specify group by gid
?>

Note:

  • The chown() function cannot operate on remote files. The file being checked must be accessible through the server's file system.

  • When safe mode is enabled, PHP will check whether the file or directory being operated on has the same UID (owner) as the script being executed.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to change file owner in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn