Home > php教程 > PHP开发 > body text

php is_writable determines whether a file can be written. Example code

高洛峰
Release: 2016-12-28 15:56:25
Original
1467 people have browsed it

php is_writable function introduction

is_writable — Determine whether the given file name is writable. The result of this function will be cached. Please use clearstatcache() to clear the cache.

Syntax:

bool is_writable (string $filename)

Returns TRUE if the file exists and is writable. The filename parameter can be a directory name that allows writability checking.

Remember that PHP may only be able to access files under the username running the webserver (usually 'nobody'). Does not count towards Safe Mode limits.

Parameters:

filename The name of the file to be checked.

Return value:

Returns TRUE if the file filename exists and is writable.

php is_writable instance

Use the is_writable function to determine whether a given file is readable:

<?php
$filename = "test.text";
if (is_readable($filename)) {
  echo "文件 $filename 可读";
} else {
  echo "文件 $filename 不可读";
}
?>
Copy after login

In fact, we can also write a function ourselves to determine whether the file is readable, and There is no need to use PHP's built-in function is_writable. The following functions can be used to replace PHP's built-in is_writable function. You can refer to it:

//可用于替换php内置的is_writable函数
function isWritable($filename){
  if(preg_match(&#39;/\/$/&#39;,$filename)){
    $tmp_file=sprintf(&#39;%s%s.tmp&#39;,$filename,uniqid(mt_rand()));
    return isWritable($tmp_file);
  }
  if(file_exists($filename)){
    //文件已经存在的话,使用读写方式打开
    $fp=@fopen($filename,&#39;r+&#39;);
    if($fp){
      fclose($fp);
      return true;
    }
    else{
      return false;
    }
  }
  else{
    $fp=@fopen($filename,&#39;w&#39;);
    if($fp){
      fclose($fp);
      unlink($filename);
      return true;
    }
    else{
      return false;
    }
  }
}
Copy after login

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more articles related to php is_writable judging whether a file can be written and example code, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!