函数文档

win_is_writable()

💡 云策文档标注

概述

win_is_writable() 函数是 WordPress 中用于解决 Windows 系统下 PHP 的 is_writable() 函数因 ACL 问题无法准确判断目录可写性的替代方案。它通过尝试打开文件来检测写入能力,而非依赖 PHP 对操作系统 ACL 的解释。

关键要点

  • 函数旨在绕过 Windows 中 PHP 的 is_writable() 函数在处理 ACL 时的已知 bug,相关 bug 报告可参考 PHP 官方链接。
  • 参数 $path 为必需字符串,表示要检查可写性的 Windows 路径。
  • 返回值为布尔类型,指示路径是否可写。
  • 函数逻辑:如果路径以 '/' 结尾或为目录,则递归检查目录内的随机临时文件;否则,尝试以追加模式打开文件来测试写入能力,并在必要时删除临时文件。
  • 自 WordPress 2.8.0 版本引入,位于 wp-includes/functions.php 文件中。
  • 相关用途:被 iis7_save_url_rewrite_rules() 和 wp_is_writable() 等函数调用,用于在 Windows 环境下确保文件系统操作的可靠性。

代码示例

function win_is_writable( $path ) {
    if ( '/' === $path[ strlen( $path ) - 1 ] ) {
        // If it looks like a directory, check a random file within the directory.
        return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp' );
    } elseif ( is_dir( $path ) ) {
        // If it's a directory (and not a file), check a random file within the directory.
        return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
    }

    // Check tmp file for read/write capabilities.
    $should_delete_tmp_file = ! file_exists( $path );

    $f = @fopen( $path, 'a' );
    if ( false === $f ) {
        return false;
    }
    fclose( $f );

    if ( $should_delete_tmp_file ) {
        unlink( $path );
    }

    return true;
}

📄 原文内容

Workaround for Windows bug in is_writable() function

Description

PHP has issues with Windows ACL’s for determine if a directory is writable or not, this works around them by checking the ability to open files rather than relying upon PHP to interpret the OS ACL.

See also

Parameters

$pathstringrequired
Windows path to check for write-ability.

Return

bool Whether the path is writable.

Source

function win_is_writable( $path ) {
	if ( '/' === $path[ strlen( $path ) - 1 ] ) {
		// If it looks like a directory, check a random file within the directory.
		return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp' );
	} elseif ( is_dir( $path ) ) {
		// If it's a directory (and not a file), check a random file within the directory.
		return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
	}

	// Check tmp file for read/write capabilities.
	$should_delete_tmp_file = ! file_exists( $path );

	$f = @fopen( $path, 'a' );
	if ( false === $f ) {
		return false;
	}
	fclose( $f );

	if ( $should_delete_tmp_file ) {
		unlink( $path );
	}

	return true;
}

Changelog

Version Description
2.8.0 Introduced.