sKunKbad Posted March 21, 2014 Share Posted March 21, 2014 A lot of times I will need to use Wordpress on sites that are on shared hosting. FTPing all of the files up to the server can take a long time, so I thought I'd just make a script to get the zip from wordpress.org, and then unpack it. I can use PHP's ZipArchive class to do this, BUT I want to remove the outermost directory called "wordpress", and that's where I've run into some trouble. Files that are in subdirectories of "wordpress" do not get copied, and I get errors that the files don't exist. I'd appreciate any help, or if there is a better way, please suggest. class WP_unpack { public function __construct() { // If file not already downloaded, download it if( ! file_exists( __DIR__ . '/wp.zip') ) { file_put_contents( __DIR__ . '/wp.zip', file_get_contents('http://wordpress.org/latest.zip') ); } // Open up the zip $zip = new ZipArchive; if( $zip->open( __DIR__ . '/wp.zip') ) { for( $i = 0; $i < $zip->numFiles; $i++ ) { $filename = $zip->getNameIndex($i); $fileinfo = pathinfo($filename); if( $filename != 'wordpress/' ) { // Remove wordpress/ from the beginning of the filename if( 0 === strpos( $filename, 'wordpress/' ) && isset( $fileinfo['extension'] ) ) { $new_filename = substr($filename, strlen('wordpress/')).''; copy( 'zip://' . __DIR__ . '/wp.zip#' . $filename, __DIR__ . '/' . $new_filename ); } } } $zip->close(); echo 'success'; } else { echo 'fail'; } } } new WP_unpack(); Quote Link to comment https://forums.phpfreaks.com/topic/287158-trouble-unpacking-zip-special-case/ Share on other sites More sharing options...
Solution sKunKbad Posted March 21, 2014 Author Solution Share Posted March 21, 2014 I figured it out. I just needed to use mkdir for the dirs that didn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/287158-trouble-unpacking-zip-special-case/#findComment-1473487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.