How to's

Unix

Copy Files on Unix Using scp (Secure Copy):

Using the unix command scp is way more reliable than any ftp client I've ever used. Just open up a shell and type:

scp -r -P 22 username@domainname.com:remotedirectory completelocalpath

to copy the remote directory remotedirectory on the remote host domainname.com to the directory completelocalpath on your local computer.

The -r means recursive since i want the entire directory with all it's contents copied.

The -P isn't always necessary, but if your ssl port is not the default, you'll need to specify it.

Creating a tar file:

tar -cvvf file.tar myfile.txt

In the above example the system would create a tar named file.tar in the directory you currently are in. Wildcards could also be used in this command, for example: tar -cvvf file.tar *.txt would compress all txt files in the current directory.

tar -cvvf home.tar home/

In the above example command the system would create a tar file named home.tar in the directory you currently are in of the home directory.

Extracting the files from a tar file:

tar -xvvf myfile.tar

In the above example command the system would uncompress (untar) the myfile.tar file in the current directory.

tar -xvvzf myfile.tar.gz

In the above example command the system would uncompress (untar) the myfile.tar.gz file in the current directory.

Grep search recursively for something

usage: grep [options] PATTERN [FILE...]
example: % grep -R 'style' ./

The example above will search the current working directory recursively for the pattern "style" returning a list of occurrences the file path, colon, preview of the file where the pattern was found.
The -R means search this directory and all directories beneath it.
The ./ means to search where I am now.

example: % grep -r -i -l 'style is dead' ./

The above example searches from the current location recursively through the directory structure (-r) for the phrase 'style is dead' ignoring case (-i) and only outputing a list of files (-l).

301 redirect one domain to another using .htaccess

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.desireddomain.com [NC]
RewriteRule ^(.*)$ http://www.desireddomain.com/$1 [R=301,L]

The first line has to be enabled for any of this to work. While it usually is enabled by default, it can't hurt to include.
Rewrite Engine on starts the engine that does the work for us.
RewriteCond looks for a condition present. In this case we are looking for the server variable containing the domain name part of a url %{HTTP_HOST}. This example checks to see if it is NOT www.desiredomain.com !^www.desireddomain.com (case insensitively) [NC] and applies rewrite rules following it until it hits one containing [L], which stops rewrite rules based on this condition. You can have multiple rules. They happen in the order they are listed.

Now for the rule:
The rule shown tranlated means, look for the start the URL ^ to the end of the line $. Remember what follows our condition (.*). Replace our condition with http://www.desireddomain.com/ and put what followed it, back $1.
R=301 makes this a permanent redirect for Search Engine friendly redirection, L means stop redirection for this condition.

So anything hitting this site with "http://www.parkedaliaseddomain.com/myfancypage.html" would be redirected to "http://www.desireddomain.com/myfancypage.html"

CSV file to PHP Array

$file = "thefiletoopen.csv";
$handle = fopen($file, "r");
$allitems = array(); // array to hold all the info
while (($data = fgetcsv($handle, 8090, ",")) !== FALSE) {
	// add each line as an array to the $allitems array 
	$allitems[$data[0]] = $data;
	
}
fclose($handle);

At this point you have created an array of all the items stored in the $allitems array. Lets say your CSV file was a list of people with the fields, "name", "phone", "email". If the name field of one person was, "Joe", you can access this info by:

$name = $allitems['Joe'][1];
$phone = $allitems['Joe'][2];
$email = $allitems['Joe'][3];

If you wanted to pass the name as a query string to a web page that read this infor you would just do:

if(isset ($_GET['name'])) {
	$file = "thefiletoopen.csv";
	$handle = fopen($file, "r");
	$allitems = array(); // array to hold all the info
	while (($data = fgetcsv($handle, 8090, ",")) !== FALSE) {
		// add each line as an array to the $allitems array
		$allitems[$data[0]] = $data; 
	}
	fclose($handle);
	// check to make sure it's in the csv file
	if($name){
		$name = $allitems[$name][1];
		$phone = $allitems[$name][2];
		$email = $allitems[$name][3];
	}
}

PHP Array to Flash Array

You're basically converting your PHP Array into a string that's built in the same structure as an array or object.

1D PHP Array:
Array (
	[0] => 1     
	[1] => 1     
	[2] => 1     
	[3] => 13     
	[4] => 1     
	[8] => 1     
	[9] => 19 
) 
String to pass to Flash

"1,1,1,13,1,1,19"

2D PHP Array
array (
	[0] => array("ham","orange", "lemon", "banana", "catsup" , "apple"),     
	[1] => array(1, 2, 3, 4, 5, 6)     
) 
2D String to pass to Flash

"ham,orange, lemon,banana,catsup ,apple;1, 2, 3, 4, 5, 6"
note the semicolon that separates the two array rows. you can use anything to separate values, you just need o create a string that's organized in this fashion.

Step 1: Turn the PHP Array into a String

here's my array:
$thearray = array (
	[0] => array("ham","orange", "lemon", "banana", "catsup" , "apple"),     
	[1] => array(1, 2, 3, 4, 5, 6)     
) 
I'll walk through the array and build my string:
// set up the string first
$thearray ="";
// walk through the array and build the string
foreach ($thearray as $v1) { // 1st D
	foreach ($v1 as $v2) { // 2nd D
		// builds each 2nd D array with it's elements separated by commas
		$thearray .= urlencode($v2).","; 
	}
	// separate each 2D line with a semicolon
	$thearray .= ";";
}

I know how to do other stuff too. really . . . .

 

browse categories