WordPress Unpacker Script

Downloading WordPress, unzipping it on your computer, then using FTP to send it to your website hosting environment is slow. This whole process, even with a very fast internet connection, can take 10 minutes. If you’ve got a slow internet connection, you might as well go to lunch.

So I wrote a PHP script to handle all of this. All I need to do is use FTP to send a single file to the website hosting environment, then use my browser to run it. The whole process might take 30 seconds.

Update 3/25/2014: Not working on Godaddy. Should I be surprised? I’ll see what I can do about this later.

Update 3/26/2014: Now working on Godaddy. Issue was related to PHP version and lack of support for __DIR__.

<?php
/**
 * WordPress Unpacker
 * @author     Robert B Gottier (skunkbad)
 * @copyright  Copyright (c) 2014, Robert B Gottier
 * @license    MIT - http://en.wikipedia.org/wiki/MIT_License
 * @link       https://blog.skunkbad.com/wordpress/wordpress-unpacker-script
 */

// Ancient versions of PHP don't support __DIR__
if( ! defined( '__DIR__' ) )
{
	define( '__DIR__', dirname( __FILE__ ) );
}

// The WordPress zip file
$wp_zip = __DIR__ . '/wp.zip';

// If file not already downloaded, download it
if( ! file_exists( $wp_zip ) )
{
	file_put_contents( 
		$wp_zip, 
		file_get_contents('http://wordpress.org/latest.zip') 
	);
}

// Open up the zip
$zip = new ZipArchive;
if( $zip->open( $wp_zip ) )
{
	// Loop through all the files in the zip
	for( $i = 0; $i < $zip->numFiles; $i++ )
	{
		// Get filename and extension for processing
		$filename = $zip->getNameIndex($i);
		$fileinfo = pathinfo($filename);

		// Skip anything that isn't a file
		if( 
			0 === strpos( $filename, 'wordpress/' ) && 
			isset( $fileinfo['extension'] ) 
		)
		{
			// Create the new file name (path)
			$new_filename = substr( $filename, strlen('wordpress/') );

			// Set target (file in zip), and destination (final location)
			$target = 'zip://' . $wp_zip . '#' . $filename;
			$destination = __DIR__ . '/' . $new_filename;

			// Make sure all the directories exist for the destination
			if( ! is_dir( dirname( $destination ) ) )
			{
				mkdir( dirname( $destination ), 0755, TRUE );
			}

			// Copy the file to the destination
			copy( $target, $destination );
		}
	}

	// Close the zip
	$zip->close();

	// Delete the zip file
	unlink( $wp_zip );

	// Display confirmation that process completed
	echo 'Process Completed';
}
else
{
	// FAIL!
	echo 'FAIL';
}

Make sure to remove the script from your website hosting environment after your run it.

One thought on “WordPress Unpacker Script”

Comments are closed.