Skip to content

Instantly share code, notes, and snippets.

@premitheme
Last active May 3, 2018 07:08
Show Gist options
  • Save premitheme/48fb64ff946f4c8786e3a01b960605c4 to your computer and use it in GitHub Desktop.
Save premitheme/48fb64ff946f4c8786e3a01b960605c4 to your computer and use it in GitHub Desktop.
Extract zip files on the server on the fly and chmod

Extract zip files on the server on the fly and chmod

Upload zip file (ex. WordPress package) and extract.php to the same folder, then type the following:

http://path/to/extract.php?file=filename.zip

On success you will get "ZIP archive extracted" message.

<?php
function recursiveChmod( $path, $filePerm = 0644, $dirPerm = 0755 ) {
if ( ! file_exists( $path ) ) {
return( false );
}
if ( is_file( $path ) ) {
chmod( $path, $filePerm );
} elseif ( is_dir( $path ) ) {
chmod( $path, $dirPerm );
$foldersAndFiles = scandir( $path );
$entries = array_slice( $foldersAndFiles, 2 );
foreach( $entries as $entry ) {
recursiveChmod( $path.DIRECTORY_SEPARATOR.$entry, $filePerm, $dirPerm );
}
}
return( true );
}
$file = $_GET['file'];
$zip = new ZipArchive();
if ( $zip->open( $file ) === TRUE ) {
$zip->extractTo( __DIR__ );
$zip->close();
recursiveChmod( __DIR__, 0644, 0755 );
echo 'ZIP archive extracted';
} else {
echo 'ZIP archive extraction failed';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment