Tuesday, May 20, 2014

Python FTPLIB Simple Example

#!/usr/bin/python
from ftplib import FTP
import sys

print "hello world"
site = 'ftp.example.com'
ftp = FTP(site)
print 'Logging in to '+site
print ftp.login('myname', 'passwd')
directory = '~/public_html/'
print 'Changing to ' + directory
print ftp.cwd(directory)
for myfile in sys.argv:
ftp.storbinary("STOR " + myfile, file(myfile, "rb"))
print ftp.retrlines('LIST')
print ftp.close()

Monday, April 28, 2014

HTML5 FOR IE

https://code.google.com/p/html5shiv/

Get City State from Hostip.info

$test=false;
$ip='63.172.12.36';
$ip='12.215.42.19';
$test ? $ip='12.215.42.19' : $ip=$_ENV['REMOTE_ADDR'] ;
$lines = explode("\n", str_replace("\r", "", file_get_contents("http://api.hostip.info/rough.php?ip=".$ip)));
$city=explode(':',$lines[2]);
preg_match('/Unknown/i', $city[1], $matches);
count($matches)==0 ? $print=true : $print=false;
if($print) { print "Hello ".$city[1]; }


Thursday, January 2, 2014

Strip newlines from entries in MySQL

update `org` set `orgName` = replace(`orgName`,"\n","");

org is the table, orgName is the column.

Thursday, December 12, 2013

PHP Array of days and checkbox

$days=array('Sunday', 
            'Monday', 
            'Tuesday', 
            'Wednesday', 
            'Thursday',
            'Friday',
            'Saturday');
foreach($days as $day) { 
    echo "<input type=\"checkbox\" 
              name=\"".$day."\" 
              value=\"".$day."\">".$day;   
   }

Tuesday, December 10, 2013

PHP image resizer

I've left code in this example specific to the project just to show more lessons than just image resizing. Core lesson is in the code lines with getimagesize and desiredSize variable. Cheers.

<?php
print "<html>\n<head>\n<title>photo server</title>\n</head>\n<body>\n";
if(isset($_REQUEST['workorderNumber'])) { $wo=$_REQUEST['workorderNumber'];}
 else { $wo="000000"; }
$files = glob(getcwd()."/*".$wo."*");
$iter=0;
print "\n<table border=\"true\"><tr>\n";
foreach($files as $file) {
 $url="http://".$_SERVER["SERVER_NAME"]."/uploads/";
 $sizeimg=1; 
 $size = getimagesize(getcwd()."/".basename($file));
 $width = $size[0]; // Index 0
 $height = $size[1]; // Index 1
 $type = $size[2]; // Index 2
 $desiredSize = 250;
 while(($height*$sizeimg) > $desiredSize) { $sizeimg= $sizeimg - .05;  }
 $nwidth=$width*$sizeimg;
 $nheight=$height*$sizeimg;
 echo "<td valign=\"top\">\n";
 print "\n<a href=\"".$url.basename($file)."\" target=\"_blank\">\n";
 print "\n<img src=\"".$url.basename($file)."\" width=\"".$nwidth."\" height=\"".$nheight."\" />\n";
 print "\n</a><br />\n"; 
 $fileArray=explode("_",basename($file));
 print $fileArray[0]." ".$fileArray[1]." ".$fileArray[5]." ".$fileArray[6]."\n</td>\n";     
 $iter++;
 if(($iter%5)==0) { print "\n</tr><tr>\n" ;}
}
?>
</tr></table>
</body>
</html>

Friday, October 18, 2013

Validate email with jQuery and my own hack


// validate email function, after function other code relative to its use
// 

function validateEmail(x)
{
var tld="";
console.log(x);
var splitAt = x.split("@");
if(!splitAt[1]) {return false;}
var username = splitAt[0];
var domainTLD = splitAt[1].split(".");
var domain=domainTLD[0];
for(var i=1;i<5;i++) {
   if(domainTLD[i]) { console.log(i +" "+ domainTLD[i]); tld=domainTLD[i] ;}
}
var usernameLength = username.length;
var domainLength = domain.length;
var tldLength = tld.length;
msg=username;
console.log(msg);
msg=domain;
console.log(msg);
msg=tld;
console.log(msg);
msg=usernameLength;
console.log(msg);
msg=domainLength;
console.log(msg);
msg=tldLength;
console.log(msg);
if ( tldLength < 3 || tldLength > 3 || domainLength < 2 || usernameLength < 1 || /\d/.test(tld) )
  {
  console.log(x + " not a valid e-mail address");
  return false;
  } 
else {
  return true;
     }
}

// just some vars not pertinent to email validation
var timedFade = 1000;
var msg="";

// document ready code block below
 
$(document).ready(function(){
msg="document.ready"; console.log(msg); msg="";

$( "#email" ).blur(function() {
var email=$( "#email" ).val();
console.log(email);
 if (validateEmail(email)) { 
  console.log("good email");  
  $("#email-status-message").text("");
  $("#email-status-message").fadeOut(timedFade); 
       } 
else { 
  console.log("bad email"); 
  $("#email-status-message").text("please enter valid email");  
  $("#email-status-message").fadeIn(timedFade); 
 }
});


})