Mount SSH Shares

Posted March 15th, 2012 in code, how to by cxjtc

Vi sucks. As much as I love using the command line, I hate writing code there. I used to like it before I started using an IDE but now Im spoiled and I dont want to use anything else. I have to do most of my work via SSH so I found a way to mount the remote server and edit using an IDE.

Install SSHFS

sudo apt-get install sshfs

Add fuse to /etc/modules

sudo nano /etc/modules

Add yourself to the ‘fuse’ group

sudo adduser username fuse

Create a mountpoint and give yourself ownership

sudo mkdir /media/mountname
sudo chown username /media/mountname

Mount the filesystem

sshfs remoteserver:/remotefolder /media/mountname

Unmount the filesystem

fusermount -u /media/mountname

Since I have so many servers to log into I wrote a small bash script to make it easier to mount and umount

#!/bin/bash

sshfs $1.server.com:/var/www /media/$1

#!/bin/bash

fusermount -u /media/$1

Directions from Ubuntu Forums.

Random Image on WebPage

Posted February 7th, 2012 in code by cxjtc

I was doing the home page of my site today and couldnt decide which image to put there. So I got a bunch of them and set it to show a random image on page load. Heres how I did it:

first create a directory in which you want to put your images:

$ mkdir frontcycle

then put all your pics in that folder.

$ cp pic1.jpg frontcycle/

next you write a php function. you can do this in an external page or right in your file.

function get_random_image() {
   $dh = opendir( "path/to/frontcycle" );

   $files = array();

   while ( $file = readdir( $dh ) ) {
       if ( preg_match( "/\.(jpg|jpeg|gif|png)$/i", $file ) ) { 
           // add or remove file extensions if you want
           $files[] = $file;
       }
   }

   return $files[ rand( 0, sizeof( $files ) - 1 ) ];
}

finally, in the page your image will be displayed, add the following code:

<img src='/path/to/frontcycle/<?php echo get_random_image() ?>' />

Copy/Paste from XTerm

Posted January 31st, 2012 in how to by cxjtc

Users of XTerm know all too well the frustrations caused by the inability to copy something in XTerm and paste it somewhere else, and vice versa. You can hack and recompile XTerm to solve this problem, but heres a way enable copy/paste as you launch:

/usr/bin/xterm -xrm 'XTerm*VT100.translations: #override <Btn1Up>: 
select-end(PRIMARY, CLIPBOARD, CUT_BUFFER0)' -e 'ssh user@server'

Send Email With an Attachment in PHP

Posted January 31st, 2012 in code by cxjtc


// basic information
$to = "mailbox@jenpeters.com";

$from = "noreply@jenpeters.com";

$subject = "This is an email";

$body = "This is some text";

// get file information
$file = "/home/cxjtc/file.csv";

$filename = basename( $file );

$contenttype = "text/csv"; // change to suit your attachment

$fh = fopen( $file, "r" );

$content = fread( $fh, filesize( $file ) );

fclose( $fh );

$content = chunk_split( base64_encode( $content) );

$headers = <<<EOT
From: $from\n
MIME-Version: 1.0\n
Content-Type: multipart/mixed; boundary="$boundary"\n\n
This is a multi-part message in MIME format.\n
--$boundary\n
Content-Type: text/plain; charset=iso-8859-1\n
Content-Transfer-Encoding: 7bit\n\n
$body\n\n
--$boundary\n
Content-Type: text/csv; name="$filename"\n
Content-Transfer-Encoding: base64\n
Content-Disposition: attachment; filename="$filename"\n\n
$content\n\n
--$boundary--
EOT

mail( $to, $subject, "", $headers );