search
Homephp教程PHP开发php is_writable determines whether a file can be written. Example code

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 不可读";
}
?>

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;
    }
  }
}

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools