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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),