Php is_writable checks whether the file can write instance code,

Source: Internet
Author: User

Php is_writable checks whether the file can write instance code,

Introduction to php is_writable Functions

Is_writable-determines whether a given file name can be written. The result of this function will be cached. 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 write check.

Remember, PHP may only use the user name (usually 'nobody') that runs webserver to access the file. This parameter is not included in the security mode.

Parameters:

The name of the file to be checked.

Return Value:

Returns TRUE if the filename file exists and can be written.

Php is_writable instance

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

<? Php $ filename = "test. text "; if (is_readable ($ filename) {echo" File $ filename readable ";} else {echo" File $ filename unreadable ";}?>

In fact, we can also write a function to determine whether the file is readable without using the php built-in function is_writable. The following functions can be used to replace the built-in is_writable function in php. For details, refer:

// It can be used to replace the built-in is_writable function isWritable ($ filename) {if (preg_match ('/\/$/', $ filename) of php )) {$ tmp_file = sprintf ('% s. tmp ', $ filename, uniqid (mt_rand (); return isWritable ($ tmp_file);} if (file_exists ($ filename) {// if the file already exists, enable $ fp = @ fopen ($ filename, 'r + ') in read/write mode; if ($ fp) {fclose ($ fp); return true ;} else {return false ;}} else {$ fp = @ fopen ($ filename, 'w'); if ($ fp) {fclose ($ fp ); unlink ($ filename); return true;} else {return false ;}}}

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.