wp_delete_file_from_directory

函数
wp_delete_file_from_directory ( $file, $directory )
参数
  • (string) $file Absolute path to the file to delete.
    Required:
  • (string) $directory Absolute path to a directory.
    Required:
返回值
  • (bool) True on success, false on failure.
定义位置
相关方法
wp_delete_filewp_delete_categorywp_delete_comment_wp_get_image_size_from_metaget_template_directory
引入
4.9.7
弃用
-

wp_delete_file_from_directory:此函数从WordPress文件系统中的特定目录中删除文件。它将文件路径和目录路径作为参数,并从指定的目录中删除文件。

如果一个文件的路径在给定的目录内,则删除该文件。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_delete_file_from_directory( $file, $directory ) {
if ( wp_is_stream( $file ) ) {
$real_file = $file;
$real_directory = $directory;
} else {
$real_file = realpath( wp_normalize_path( $file ) );
$real_directory = realpath( wp_normalize_path( $directory ) );
}
if ( false !== $real_file ) {
$real_file = wp_normalize_path( $real_file );
}
if ( false !== $real_directory ) {
$real_directory = wp_normalize_path( $real_directory );
}
if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) {
return false;
}
wp_delete_file( $file );
return true;
}
function wp_delete_file_from_directory( $file, $directory ) { if ( wp_is_stream( $file ) ) { $real_file = $file; $real_directory = $directory; } else { $real_file = realpath( wp_normalize_path( $file ) ); $real_directory = realpath( wp_normalize_path( $directory ) ); } if ( false !== $real_file ) { $real_file = wp_normalize_path( $real_file ); } if ( false !== $real_directory ) { $real_directory = wp_normalize_path( $real_directory ); } if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) { return false; } wp_delete_file( $file ); return true; }
function wp_delete_file_from_directory( $file, $directory ) {
	if ( wp_is_stream( $file ) ) {
		$real_file      = $file;
		$real_directory = $directory;
	} else {
		$real_file      = realpath( wp_normalize_path( $file ) );
		$real_directory = realpath( wp_normalize_path( $directory ) );
	}

	if ( false !== $real_file ) {
		$real_file = wp_normalize_path( $real_file );
	}

	if ( false !== $real_directory ) {
		$real_directory = wp_normalize_path( $real_directory );
	}

	if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) {
		return false;
	}

	wp_delete_file( $file );

	return true;
}

常见问题

FAQs
查看更多 >