PHP中的is_readable()函数用于检查指定的文件是否存在以及是否可读。文件名作为参数发送到is_readable()函数,如果文件存在且可读,则返回True。
is_readable()函数为流返回False,例如php://stdin。
is_readable()函数也可以与某些URL包装一起使用,例如PHP 5.0.0中的file://,http://,ftp://,php://。
用法:
is_readable($file)
使用的参数:
PHP中的is_readable()函数仅接受一个参数。
- file:它是指定文件的必需参数。
返回值:
如果指定的文件或目录存在并且可读,则返回True;否则返回False。
异常:
- 失败时发出E_WARNING。
- 此函数的结果被缓存,因此使用clearstatcache()函数清除缓存。
以下示例程序旨在说明is_readable()函数。
程序1:
<?php  
$myfile = "gfg.txt"; 
  
// checking whether file is readable or not 
if (is_readable($myfile))  
{ 
    echo '$myfile is readable'; 
}  
else 
{ 
    echo '$myfile is not readable'; 
} 
?>输出:
gfg.txt is readable
程序2:
<?php  
$myfile = "gfg.txt"; 
  
// checking whether file is readable or not 
if (is_readable($myfile))  
{ 
    echo '$myfile is readable'; 
  
    // displaying contents of the uploaded file 
    echo "Contents of the file are:\n"; 
    readfile($myfile); 
}  
else 
{ 
    echo '$myfile is not readable'; 
} 
?>输出:
gfg.txt is readable Contents of the file are: Portal for geeks!
程序3:
<?php  
  
$permissions = fileperms("gfg.txt"); 
$permvalue = sprintf("%o", $permissions); 
  
// Clearing the File Status Cache  
clearstatcache(); 
  
if(is_readable("gfg.txt"))  
{  
   echo("File is Readable  
    and File Permissions are:$permvalue)"); 
}  
else
{ 
  echo("File is Not Readable  
     and File Permissions are:$permvalue)"); 
}  
  
// Clearing the File Status Cache 
clearstatcache(); 
?>输出:
File is Readable and File Permissions are:0644
相关文章: PHP - is_writable()用法及代码示例
参考:
http://php.net/manual/en/function.is-readable.php
相关用法
- PHP SplFileInfo isReadable()用法及代码示例
- PHP DirectoryIterator isReadable()用法及代码示例
- d3.js d3.set.has()用法及代码示例
- p5.js sin()用法及代码示例
- p5.js tan()用法及代码示例
- p5.js arc()用法及代码示例
- p5.js log()用法及代码示例
- p5.js cos()用法及代码示例
- p5.js red()用法及代码示例
- PHP Ds\Map map()用法及代码示例
- PHP Ds\Map last()用法及代码示例
- p5.js second()用法及代码示例
注:本文由纯净天空筛选整理自Shubrodeep Banerjee大神的英文原创作品 PHP | is_readable() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
