Skip to content

Commit a4ba857

Browse files
Merge pull request #1338 from mrrobot47/add/download-unzip
Add utility functions for download and unzip
2 parents 4c713b2 + 8203ea2 commit a4ba857

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

php/utils.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,44 @@ function get_config_value( $key, $default = null ) {
16451645
return empty( $existing_config[ $key ] ) ? $default : $existing_config[ $key ];
16461646
}
16471647

1648+
/**
1649+
* Function to download file to a path.
1650+
*
1651+
* @param string $path Path to download the file on.
1652+
* @param string $download_url Url to download the file from.
1653+
*/
1654+
function download( $path, $download_url ) {
1655+
1656+
$headers = array();
1657+
$options = array(
1658+
'timeout' => 1200, // 20 minutes ought to be enough for everybody.
1659+
'filename' => $path,
1660+
);
1661+
http_request( 'GET', $download_url, null, $headers, $options );
1662+
}
1663+
1664+
/**
1665+
* Extract zip files.
1666+
*
1667+
* @param string $zip_file Path to the zip file.
1668+
* @param string $path_to_extract Path where zip needs to be extracted to.
1669+
*
1670+
* @return bool Success of extraction.
1671+
*/
1672+
function extract_zip( $zip_file, $path_to_extract ) {
1673+
1674+
$zip = new \ZipArchive;
1675+
$res = $zip->open( $zip_file );
1676+
if ( true === $res ) {
1677+
$zip->extractTo( $path_to_extract );
1678+
$zip->close();
1679+
1680+
return true;
1681+
}
1682+
1683+
return false;
1684+
}
1685+
16481686
/**
16491687
* Random name generator.
16501688
*

0 commit comments

Comments
 (0)