Convert the file permissions from stat () in PHP to the Linux form

Source: Internet
Author: User

The stat function of PHP can return various properties of the file, see: http://php.net/manual/zh/function.stat.php

Where the Mode field represents the file's permissions, a numeric value of type int. The function below is to convert the int value to the format of the Linux form, such as 33206--rw-rw-rw-

1 /**2 * [Return-rwxrwxrwx file permission expression]3 * @param string $filePath [file path]4 * @return [string] []5  */6 functionGethumanfilestat ($filePath= ' ')7 {8     if(!file_exists($filePath) || !Is_file($filePath))9         return false;Ten  One     Clearstatcache(); A     $stat=Stat($filePath); -     $mode=$stat[' Mode ']; -  the     //reference libc ' s Fstat (): Http://man.he.net/man2/fstat - //8 file types for binary values -     $typeMap=Array( -0140000 = ' Ssocket ', +0120000 = ' Llink ', -0100000 = '-file ', +0060000 = ' bblock ', A0040000 = ' Ddirectory ', at0020000 = ' CChar ', -0010000 = ' Pfifo ', -     ); -  -     //8 binary values corresponding to each bit's permissions -     $bitValueMap=Array( in' Mask ' = 0170000, -' Ubit ' = 0004000, to' Gbit ' = 0002000, +' Stickybit ' = 0001000, -' Ur ' = 0400, the' UW ' = 0200, *' UX ' = 0100, $' gr ' = 0040,Panax Notoginseng' GW ' = 0020, -' GX ' = 0010, the' or ' = 0004, +' Ow ' = 0002, A' Ox ' = 0001, the     ); +  -     $str= ' '; $     //Filetype,first character $     $str.=array_key_exists($mode&$bitValueMap[' Mask '],$typeMap) ?substr($typeMap[$mode&$bitValueMap[' Mask ']], 0, 1): ' U '; -  -     //User Permission the     $str.=$mode&$bitValueMap[' ur ']? ' R ': '-'; -     $str.=$mode&$bitValueMap[' UW ']? ' W ': '-';Wuyi     $str.=$mode&$bitValueMap[' UX ']? ($mode&$bitValueMap[' Ubit ']? ' s ': ' X '): ($mode&$bitValueMap[' Ubit ']? ' S ': '-'); the  -     //Group Permission Wu     $str.=$mode&$bitValueMap[' Gr ']? ' R ': '-'; -     $str.=$mode&$bitValueMap[' GW ']? ' W ': '-'; About     $str.=$mode&$bitValueMap[' GX ']? ($mode&$bitValueMap[' Gbit ']? ' s ': ' X '): ($mode&$bitValueMap[' Gbit ']? ' S ': '-'); $  -     //Other Permission -     $str.=$mode&$bitValueMap[' or ']? ' R ': '-'; -     $str.=$mode&$bitValueMap[' Ow ']? ' W ': '-'; A     $str.=$mode&$bitValueMap[' Ox ']? ($mode&$bitValueMap[' Stickybit ']? ' t ': ' X '): ($mode&$bitValueMap[' Stickybit ']? ' T ': '-'); +  the     Clearstatcache(); -     return $str; $}

Reference documents:

http://php.net/manual/zh/function.stat.php

Http://technosophos.com/2012/02/15/php-mode-strings-stat-and-stream-wrappers.html

Http://man.he.net/man2/fstat

Convert the file permissions from stat () in PHP to the Linux form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.