win_is_writable

函式
win_is_writable ( $path )
引數
  • (string) $path Windows path to check for write-ability.
    Required:
返回值
  • (bool) Whether the path is writable.
相關
  • https://bugs.php.net/bug.php?id=27609
  • https://bugs.php.net/bug.php?id=30931
定義位置
相關方法
wp_is_writableis_iterablewp_iso_descrambler_get_list_tablewp_is_mobile
引入
2.8.0
棄用
-

win_is_writable: 這個函式用來檢查一個檔案或目錄在Windows系統中是否可寫。它需要一個引數–$path–它是要檢查的檔案或目錄的路徑。

解決is_writable()函式中的Windows bug

PHP在確定一個目錄是否可寫時與Windows的ACL有問題,這個方法通過檢查開啟檔案的許可權來解決,而不是依靠PHP來解釋作業系統的ACL。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >