Jump to content

Trouble unpacking zip (special case)


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
https://forums.phpfreaks.com/topic/287158-trouble-unpacking-zip-special-case/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.