Skip to content

Instantly share code, notes, and snippets.

@thagxt
Last active February 23, 2022 18:45
Show Gist options
  • Save thagxt/d9b4388156aeb7f1d66b108d728470d2 to your computer and use it in GitHub Desktop.
Save thagxt/d9b4388156aeb7f1d66b108d728470d2 to your computer and use it in GitHub Desktop.
Download & Extract .zip with cURL directly on your server
<?php
$url = "https://wordpress.org/latest.zip"; // URL of what you wan to download
$zipFile = "wordpress.zip"; // Rename .zip file
$extractDir = "extracted"; // Name of the directory where files are extracted
$zipResource = fopen($zipFile, "w");
// Get The Zip File From Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
if(!$page) {
echo "Error :- ".curl_error($ch);
}
curl_close($ch);
/* Open the Zip file */
$zip = new ZipArchive;
$extractPath = $extractDir;
if($zip->open($zipFile) != "true"){
echo "Error :- Unable to open the Zip File";
}
/* Extract Zip File */
$zip->extractTo($extractPath);
$zip->close();
die('Your file was downloaded and extracted, go check.');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment