Learning Is Fun

Talks on Web Technology and Better Product Development

WordPress: Upload many PHP files quickly by Zip & Unzip

December15

Hello!

It is actually not only for a wordpress developer but also for all those developers who face situation where they must upload many files to the server. Recently in a wordpress project, I had to upload wordpress 2.7 version files in the server. In the wordpress 2.7 package, you will find around 600 files! Uploading them is not very interesting when your internet connection do not guarantee you smooth upload.

Uploading the 600 files brought me pain because they were going to the server very slowly and each time some files were corrupted and for any reason reuploading the corrupted files were not working. It was really time consuming, boring and painful. Then I started to look for an alternative solution. Then, I naturally came to the idea that how about I zip (compress) the folders containing most of the files and upload them. Thus I have to upload few zip files although bigger in size. Writing a piece of PHP code to unzip the zipped file is easy too.

So, I decided to zip the main three folders and uploaded them. You know they contain most of the files.

OK. Let me tell you the process in step by step way so that you can follow easily:

  1. First I zipped my three folders (wp-admin, wp-content & wp-includes) that contains most of the files.
  2. Changed the ‘public_html‘ folder permission to 777. Remember you will do it for the folder where you are going to upload.
  3. Uploaded the three zipped folders.
  4. Run my PHP script that you will see below.
  5. Checked if everything is OK and found them OK.
  6. Changed the file permission of the ‘public_html‘ folder back to 750. If you uploaded to any other folder, you need to change the permission 777 to any combination you think good for your purpose.
  7. Deleted the zip files since they are no longer required.

Here goes my PHP code to unzip them and it is very simple!

<?php 
	$zipFileNames = array();
	$zipFileNames[] = 'wp-admin.zip';
	$zipFileNames[] = 'wp-content.zip';
	$zipFileNames[] = 'wp-includes.zip';
	// and all other zip files

	$indexLimit = count($zipFileNames);
	for($i=0; $i<$indexLimit; $i++)
	{
		$fileLink = $_SERVER['DOCUMENT_ROOT'] . '/' . $zipFileNames[$i];
		if(is_file($fileLink))
		{
			if(exec("unzip $fileLink"))
			{
				echo 'Successful! ' . $fileLink . ' is properly unzipped!' . '<br />';
			}
			else

			{
				echo 'Failed! exec() failed to execute as expected!' . '<br />' .
				'Please check if the directory permission is 777 where you are trying upload.';
			}
		}
		else

		{
			echo 'Error: ' . $fileLink . ' is not a (zip) file!' . '<br />';
		}
	}
?>

By the way, you can improve the code to more sophisticated one as per your requirement. For example, the exec() function has several options that you can use for more flexibility or anyway anything else you want.

Oh! There is one other way to unzip a uploaded zipped file although I did not use this piece of code. But remember that if you use this code you need to set your destination file permission to ’777′ first and then change as mentioned before after you unzipped. Fortunately this coding is very simple too and an example is as follows:

<?php 
	$zip = new ZipArchive;
	if($zip->open($_SERVER['DOCUMENT_ROOT'] . '/hello.zip') === true)
	{
		$zip->extractto($_SERVER['DOCUMENT_ROOT'] . '/hellotest/');
		$zip->close();
		echo 'Files successfully unzipped!';
	}
	else

	{
		die('Failed to open zip file');
	} 
?>

I tested both the processes mentioned above in Linux system and not sure how it works in Windows Server. I guess the second process should work in Windows server for uploading and unzipping bulk file quickly.

This is all for the trick of the game and for killing the pain of uploading several hundred of files.

Well. You can download my little sweet code here too:
http://www.tanzilo.com/demo/code/unzip/unzipping.zip

Thank you for reading.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
posted under PHP, Wordpress
One Comment to

“WordPress: Upload many PHP files quickly by Zip & Unzip”

  1. On April 20th, 2009 at 2:39 am sitta Says:

    thankyou

Email will not be published

Website example

Your Comment: