search
HomeBackend DevelopmentPHP ProblemPHP determines whether a file is writable

PHP determines whether a file is writable

Sep 30, 2019 pm 05:44 PM
phpjudgmentdocument

PHP determines whether a file is writable

PHP determines whether a file or directory is writable

In PHP, the is_writable() function can be used to determine whether a file/directory is writable. , details are as follows:

Reference

is_writable — Determine whether the given file name is writable.

Description

bool is_writable (string $filename)

Returns TRUE if the file exists and is writable. (The $filename parameter can be a directory name, that is, check whether the directory is writable.)

Note:

PHP may only be able to run the webserver as the username (usually as 'nobody') to access files, does not count against safe mode restrictions.

is_writable() Example

<?php
$filename = &#39;test.txt&#39;;
if (is_writable($filename)) {
    echo &#39;The file is writable&#39;;
} else {
    echo &#39;The file is not writable&#39;;
}
?>

Note: is_writeable() is an alias of is_writable()!

In order to be compatible with various operating systems, you can customize a writable function. The code is as follows:

/**
 * 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数)
 *
 * @param string $file 文件/目录
 * @return boolean
 */
function new_is_writeable($file) {
    if (is_dir($file)){
        $dir = $file;
        if ($fp = @fopen("$dir/test.txt", &#39;w&#39;)) {
            @fclose($fp);
            @unlink("$dir/test.txt");
            $writeable = 1;
        } else {
            $writeable = 0;
        }
    } else {
        if ($fp = @fopen($file, &#39;a+&#39;)) {
            @fclose($fp);
            $writeable = 1;
        } else {
            $writeable = 0;
        }
    }
 
    return $writeable;
}

Recommended tutorial: PHP video tutorial

The above is the detailed content of PHP determines whether a file is writable. For more information, please follow other related articles on 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 Article

Hot Tools

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.