How to fix linux not sending mail problem: Sendmail stat=Deferred: Connection refused by [127.0.0.1]

I recently experienced a problem where my server was not sending mail from the PHP mail() function or the command line "mail" function. After looking through /var/log/maillog, I was able to see that the local 127.0.0.1 mail server was refusing the connection. Some Google searching landed me at the following thread, where the third post did the trick:

http://www.howtoforge.com/forums/showthread.php?t=16246

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;
}

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;

How To Convert Large XML Files to CSV

I usually struggle with converting very large XML files to other formats just because they are in a dynamic format and most programs you find run out of memory before properly parsing them.¬†Well, I’m happy to say, I found a FAST and EASY solution. Of course this will work for small files as well as big files.

You’ll want to grab a copy of the msxsl command line utility from Microsoft.

After you’ve got that, you’ll need to setup a XSL file to tell the program how to format your file. If you’re unfamiliar with XSL, you can familiarize yourself here.

After you’ve got your XSL file created, it’s a simple command line entry:

msxsl xml_file.xml xsl_file.xsl -o output_file.csv

The following is a sample XML and XSL file that I used.

XML File:

	
		772500
		Tue, 20 Jan 2009 16:28:08 CST
		Tue, 20 Jan 2009 16:51:01 CST

			61951
			The Hills Season 1

		773000
		Tue, 20 Jan 2009 16:28:08 CST
		Tue, 20 Jan 2009 16:53:54 CST

			61926
			Hogan Knows Best Season 2

		775500
		Thu, 22 Jan 2009 14:49:12 CST
		Thu, 22 Jan 2009 14:51:35 CST

			62068
			Carlos Mencia 2007

			1402
			Comedy Central

XSL File (Creates tab-delimited file)