Skip to content

Instantly share code, notes, and snippets.

@gdarko
Last active May 2, 2022 18:42
Show Gist options
  • Save gdarko/2ca320d1b447c859430e3bd6e1ee47e8 to your computer and use it in GitHub Desktop.
Save gdarko/2ca320d1b447c859430e3bd6e1ee47e8 to your computer and use it in GitHub Desktop.
Unzip php file
<?php
set_time_limit(0);
function unzipFile( $file, $dir = null ) {
if ( is_null( $dir ) ) {
$dir = dirname( __FILE__ );
}
$dir = $dir . DIRECTORY_SEPARATOR . 'unzipped';
if ( ! file_exists( $dir ) ) {
mkdir( $dir, 0777, true );
}
$zip = new ZipArchive;
$res = $zip->open( $file );
if ( $res === true ) {
$zip->extractTo( $dir );
$zip->close();
return true;
} else {
return false;
}
}
function downloadFile( $url, $dir_path ) {
$newfname = rtrim( $dir_path, '/' ) . DIRECTORY_NAME . pathinfo( $url, PATHINFO_BASENAME );
$file = fopen( $url, 'rb' );
if ( $file ) {
$newf = fopen( $newfname, 'wb' );
if ( $newf ) {
while ( ! feof( $file ) ) {
fwrite( $newf, fread( $file, 1024 * 8 ), 1024 * 8 );
}
}
}
if ( $file ) {
fclose( $file );
}
if ( $newf ) {
fclose( $newf );
}
return true;
}
$url = 'https://sardofoodsb2b.dev.ideowp.com/sf.zip';
$dir = dirname(__FILE__);
if ( downloadFile( $url, $dir ) ) {
echo 'DOWNLOADED' . '<br/>';
if(unzipFile( pathinfo( $url, PATHINFO_BASENAME ), $dir ) ) {
echo 'UNZIPPED' . '<br/>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment