您的位置 首页 php

PHP如何处理文件权限?(代码示例)

文件权限是指可以对系统中的特定文件执行的操作,例如,读取,写入或执行文件。下面本篇文章就来给大家介绍使用PHP如何进行文件权限的检查和更改,希望对大家有所帮助。

PHP如何处理文件权限?(代码示例)

PHP会在幕后自动授予适当的权限。例如,如果创建一个新文件进行编写,PHP会自动授予其读写权限。此外,PHP还提供了一些用于检查和更改文件权限的有用功能。

PHP如何检查文件权限?

PHP为我们提供了三个函数,用来检查文件权限:

is_readable()函数:用于检查是否有读取权限,如果允许读取文件,则返回true,否则返回false。

is_writable()函数:用于检查是否有写入权限,如果允许写入文件,则返回true,否则返回false。

is_executable()函数:用于检查是否有执行权限,如果允许执行文件,则返回true,否则返回false。

代码示例:

<?php

header(“content-type:text/html;charset=utf-8”);

$fn = ‘demo/test.txt’;

$ msg = is_readable($fn) ? $msg = ‘文件是可读的’

: $msg = ‘文件不可读’;

echo $msg . ‘<br/>’;

$msg = is_writable($fn) ? $msg = ‘文件是可写的’

: $msg = ‘文件不可写’;

echo $msg . ‘<br/>’;

$msg = is_executable($fn) ? $msg = ‘文件是可执行的’

: $msg = ‘文件不可执行’;

echo $msg . ‘<br/>’;

?>

输出:

PHP如何处理文件权限?(代码示例)

除了这些函数之外,php还提供fileperms()函数,该函数返回一个整数,表示对特定文件设置的权限。

<?php

$fn = ‘demo/test.txt’;

$ fp = fileperms($fn);

echo substr(sprintf(‘%o’, $fp), -4); //0666

?>

输出:

0666

PHP如何更改文件权限?

要更改文件权限或模式,需要使用 chmod ()函数。首先,需要传递要设置权限的文件名;然后,需要指定所需的权限。如果成功设置了权限,chmod()函数将返回true,否则返回false。

文件权限由包含三位数字的八进制数表示:

● 第一个数字指定文件的所有者可以对文件执行的操作。

● 第二个数字指定文件的所有者组可以对文件执行的操作。

● 第三个数字指定每个人可以对文件执行的操作。

下表说明了表示特定用户(用户,用户组或所有人)的访问权限的每个数字的值:

● 0:不能读,写或执行

● 1:只能执行

● 2:只能写

● 3:可以写和执行

● 4:只能读

● 5:可以阅读和执行

● 6:可以读写

● 7:可以读,写和执行

例如,要设置创建者或所有者、以及其他人只能读取该文件,我们使用以下代码:

<?php

$fn = ‘demo/test.txt’;

//更改权限

chmod($fn, 0444);

//读取权限

$msg = is_readable($fn) ? $msg = ‘文件是可读的’

: $msg = ‘文件不可读’;

echo $msg . ‘<br/>’;

$msg = is_writable($fn) ? $msg = ‘文件是可写的’

: $msg = ‘文件不可写’;

echo $msg . ‘<br/>’;

$msg = is_executable($fn) ? $msg = ‘文件是可执行的’

: $msg = ‘文件不可执行’;

echo $msg . ‘<br/>’;

?>

输出:

PHP如何处理文件权限?(代码示例)

注意:我们在644之前放置0,要求PHP将其解释为八进制数。

相关视频教程推荐:《PHP教程》

以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。更多精彩内容大家可以关注相关教程栏目!!!

以上就是PHP如何处理文件权限?(代码示例)的详细内容,更多请关注其它相关文章!

更多技巧请《转发 + 关注》哦!

文章来源:智云一二三科技

文章标题:PHP如何处理文件权限?(代码示例)

文章地址:https://www.zhihuclub.com/78167.shtml

关于作者: 智云科技

热门文章

评论已关闭

26条评论

  1. Anthony Elias The reason we care about micrometastatic lymph node disease is because its presence changes disease stage, prognosis, and treatment choices, including further surgery

  2. 5 ОјL of qPCR master mix containing 0 Through a series of conversion enzyme pathways chemicals that alter the molecular structure of other chemicals their bodies synthesize the necessary sex, Glucocorticoid, and Mineralocorticoid steroids

  3. To assess the role of the simultaneous presence of different risk alleles, 0, 1, or 2 points were assigned to the 6 genotypes previously described and a risk score was generated grouping the 5 polymorphisms associated with shorter PFS Eur J Cancer 1976; 12 419 424

  4. We explored the association of MTHFR C677T, A1298C and G1793A polymorphisms and risk of cancer and found only MTHFR C677T homogeneity wild type TT CC polymorphism positively associated with overall risk OR 1

  5. Inhibition of human breast cancer cell proliferation with estradiol metabolites is as effective as with tamoxifen

  6. Since these populations are at a high risk of developing PH, the generalizability of our results is limited

  7. Because of the potential for serious adverse reactions in nursing infants from Taxofen tamoxifen citrate, women taking Taxofen tamoxifen citrate should not breast feed

  8. Since Cre expression can sometimes lead to cellular toxicity, we checked for pathological phenotypes in the RPE of treated mice 12 Mason C, Xiao L, Duggan C, Imayama I, Foster Schubert KE, Kong A, Campbell KL, Wang CY, Alfano CM, Blackburn GL, Pollack M, McTiernan A 2013 Effects of dietary weight loss and exercise on insulin like growth factor I and insulin like growth factor binding protein 3 in postmenopausal women a randomized controlled trial

  9. Several drugs and devices are FDA approved for weight reduction and maintenance of weight loss

  10. Farm Hosp 2012; 36 4 256 260 When LILETTA is removed, your menstrual periods will usually return to your previous pattern

  11. This reduction is thought to be related to the fairly low, stable level of estrogen Indapamide is metabolized in the liver, therefore a dosage reduction may be required for those with liver disease or cirrhosis of the liver

  12. Sally Kydd and Dana Rowett Response magnitudes were then expressed as ratios relative to the mean of 0

  13. Originally, the number of Huaxia universities on when does a males penis grow the list what foods increase penile blood flow Male Enhancement Pills Fast Flow was poor, how long does penis grow for but now it has been completely crushed by North America 7 Analyses were carried out in either SAS version 9

  14. These drugs affect one s endocrinology by increasing the female hormone estrogen and causing the testicular hormone levels to drop

网站地图