PHP determines if a file or directory is writable

Source: Internet
Author: User

In PHP, you can use the is_writable () function to determine whether a file/directory is writable, as detailed below:

Reference

Is_writable

(PHP 4, PHP 5)

Is_writable-determines whether a given file name can be written

Description

BOOL Is_writable (String $filename)

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

Remember that PHP may only be able to access the file by running the webserver user name (usually ' nobody '). Restrictions that do not count toward Safe mode.

Example #1 is_writable () example

<?php$filename = ' Test.txt ', if (is_writable ($filename)) {echo ' The file is writable ';} else {echo ' The file was not writ Able ';}? >

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

However, the above function in the PHP4 is a bug, especially under the Windows Server is not allowed, the official related bug report link is as follows:

http://bugs.php.net/bug.php?id=27609

To be compatible with each operating system, you can customize a writable function with the following code:

/** * Determine if the file/directory is writable (replace the system's own is_writeable function) * * @param string $file File/directory * @return Boolean */function new_is_writeable ($ File) {if (Is_dir ($file)) {$dir = $file, if ($fp = @fopen ("$dir/test.txt", ' W ')) {@fclose ($fp); @unlink ("$dir/test.txt"); $ writeable = 1;} else {$writeable = 0;}} else {if ($fp = @fopen ($file, ' A + ')) {@fclose ($fp); $writeable = 1;} else {$writeable = 0;}} return $writeable;}

PHP determines if a file or directory is writable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.