unzip_file( string $file, string $to )
Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction.
Description Description
Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
Attempts to increase the PHP memory limit to 256M before uncompressing. However, the most memory required shouldn’t be much larger than the archive itself.
Parameters Parameters
- $file
-
(string) (Required) Full path and filename of ZIP archive.
- $to
-
(string) (Required) Full path on the filesystem to extract archive to.
Return Return
Source Source
File: wp-admin/includes/file.php
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 | function unzip_file( $file , $to ) { global $wp_filesystem ; if ( ! $wp_filesystem || ! is_object ( $wp_filesystem ) ) { return new WP_Error( 'fs_unavailable' , __( 'Could not access filesystem.' ) ); } // Unzip can use a lot of memory, but not this much hopefully. wp_raise_memory_limit( 'admin' ); $needed_dirs = array (); $to = trailingslashit( $to ); // Determine any parent dir's needed (of the upgrade directory) if ( ! $wp_filesystem -> is_dir ( $to ) ) { //Only do parents if no children exist $path = preg_split( '![/\\\]!' , untrailingslashit( $to ) ); for ( $i = count ( $path ); $i >= 0; $i -- ) { if ( empty ( $path [ $i ] ) ) { continue ; } $dir = implode( '/' , array_slice ( $path , 0, $i + 1 ) ); if ( preg_match( '!^[a-z]:$!i' , $dir ) ) { // Skip it if it looks like a Windows Drive letter. continue ; } if ( ! $wp_filesystem -> is_dir ( $dir ) ) { $needed_dirs [] = $dir ; } else { break ; // A folder exists, therefor, we dont need the check the levels below this } } } /** * Filters whether to use ZipArchive to unzip archives. * * @since 3.0.0 * * @param bool $ziparchive Whether to use ZipArchive. Default true. */ if ( class_exists ( 'ZipArchive' , false ) && apply_filters( 'unzip_file_use_ziparchive' , true ) ) { $result = _unzip_file_ziparchive( $file , $to , $needed_dirs ); if ( true === $result ) { return $result ; } elseif ( is_wp_error( $result ) ) { if ( 'incompatible_archive' != $result ->get_error_code() ) { return $result ; } } } // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. return _unzip_file_pclzip( $file , $to , $needed_dirs ); } |
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Example
Basic example showing how to unzip the contents of a .zip file, that resides in the WordPress upload directory, to the same destination.
<?php
WP_Filesystem();
$destination
= wp_upload_dir();
$destination_path
=
$destination
[
'path'
];
$unzipfile
= unzip_file(
$destination_path
.
'/filename.zip'
,
$destination_path
);
if
(
$unzipfile
) {
echo
'Successfully unzipped the file!'
;
}
else
{
echo
'There was an error unzipping the file.'
;
}
?>