Category Archives: Uncategorized

Simple PHP script for unzip function

Here’s a simple PHP function to unzip a ZIP file and retain directory structure – great for use when you can’t use exec(). The function will return false on error and true on success. Call it with the following code:

if (unzip('../mtm.zip'))
	echo 'Cool, it worked!';
else
	echo 'Uh oh, something went wrong.';
function unzip($zipfile) {
	$zip = zip_open($zipfile);
	if ($zip) {
		while ($item = zip_read($zip)) {
			$name = zip_entry_name($item);
			if (substr($name, -1) == '/') {
				if (!file_exists($name)) {
					$dir = mkdir($name);
					if (!$dir) {
						zip_close($zip);
						return false;
					}
				}
			} else {
				if (zip_entry_open($zip, $item, "r")) {
					$f = file_put_contents($name, zip_entry_read($item, zip_entry_filesize($item)));
					zip_entry_close($item);
					if ($f === false) {
						zip_close($zip);
						return false;
					}
				} else {
					zip_close($zip);
					return false;
				}
			}
		}
		zip_close($zip);
		return true;
	}
	return false;
}
[ad]

How to Duplicate Database Rows in MySQL

If you're looking for a quick, easy way to duplicate a MySQL Row, try the following example:

INSERT INTO Tbl1
		(Col2, Col3, ...)
	SELECT
		(Col2, Col3, ...)
	FROM Tbl1
		WHERE Col1 = 1;

This example assumes you want to duplicate the row where Col1 equals 1. You could change the where clause to anything for that matter. However, if there are more than one rows selected from the SELECT statement, it will INSERT each row, not just one.

Likewise, you could duplicate a whole MySQL table using the following:

INSERT INTO Tbl2
		(Col2, Col3, ...)
	SELECT
		(Col2, Col3, ...)
	FROM Tbl1;
[ad]

How to mount Amazon EC2 EBS volume on an instance’s file system

[ad]
The following assumes you have created a volume in AWS Management Console and attached it to your instance as /dev/sdh.
 
A brand new volume is unformatted block storage.  It shows up as another device, but doesn’t contain a filesystem.  (An EBS volume that previously had a filesystem created on it won’t be mounted either by the attachment process).
 
You can verify that your instance can see the EBS volume by running the following command on the instance:
 
cat /proc/partitions
 
You’ll see an entry for sdh.  At this point, you should create a filesystem and mount it manually.  You could create an EXT3 filesystem on the device and then mount it on ‘/mnt/ebs’ with the following commands, (run as root):
 
mke2fs -F -j /dev/sdh
mkdir /mnt/ebs

mount /dev/sdh /mnt/ebs