
什么是Localhost?以及它如何应用于WordPress?
validate_file ( $file, $allowed_files = array() )
validate_file。这个WordPress函数是用来验证一个文件的。它检查文件是否存在,是否可读,以及是否为空,如果发现任何错误,则返回错误信息。
根据允许的规则集验证文件名和路径。
返回值为`1`意味着文件路径包含目录遍历。
返回值为`2’意味着文件路径包含Windows驱动路径。
返回值为`3’意味着文件不在允许的文件列表中。
function validate_file( $file, $allowed_files = array() ) { if ( ! is_scalar( $file ) || '' === $file ) { return 0; } // `../` on its own is not allowed: if ( '../' === $file ) { return 1; } // More than one occurrence of `../` is not allowed: if ( preg_match_all( '#../#', $file, $matches, PREG_SET_ORDER ) && ( count( $matches ) > 1 ) ) { return 1; } // `../` which does not occur at the end of the path is not allowed: if ( false !== strpos( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) { return 1; } // Files not in the allowed file list are not allowed: if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files, true ) ) { return 3; } // Absolute Windows drive paths are not allowed: if ( ':' === substr( $file, 1, 1 ) ) { return 2; } return 0; }