Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Created February 26, 2021 08:13
Show Gist options
  • Save FrancoStino/a88109cacad3d5553c43c05af2ba9959 to your computer and use it in GitHub Desktop.
Save FrancoStino/a88109cacad3d5553c43c05af2ba9959 to your computer and use it in GitHub Desktop.
A simple PHP script that automatically downloads and unzips file in the current directory.
<?php
echo '<pre>';
echo '<span style="color:blue">DOWNLOADING...</span>'.PHP_EOL;
// Download file
file_put_contents('prodotti.zip', file_get_contents('ftp://5100_stano:trHfD,[email protected]:21/dati/Anagrafiche.zip'));
$zip = new ZipArchive();
$res = $zip->open('prodotti.zip');
if ($res === TRUE) {
// Extract ZIP file
$zip->extractTo('./');
$zip->close();
unlink('prodotti.zip');
// Copy files from wordpress dir to current dir
$files = find_all_files("");
$source = "/";
foreach ($files as $file) {
$file = substr($file, strlen("/"));
if (in_array($file, array(".",".."))) continue;
if (!is_dir($source.$file)){
echo '[FILE] '.$source.$file .' -> '.$file . PHP_EOL;
rename($source.$file, $file);
}else{
echo '[DIR] '.$file . PHP_EOL;
@mkdir($file);
}
}
} else {
echo 'Oops, that didn\'t work...';
}
function find_all_files($dir) {
$root = scandir($dir);
foreach($root as $value) {
if($value === '.' || $value === '..') {continue;}
$result[]="$dir/$value";
if(is_file("$dir/$value")) {continue;}
foreach(find_all_files("$dir/$value") as $value)
{
$result[]=$value;
}
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment