Jump to content

Trouble unpacking zip (special case)


sKunKbad
Go to solution Solved by sKunKbad,

Recommended Posts

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();
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.