Saturday, December 8, 2007

Using lftp in copying files between two servers in ssh

LFTP is sophisticated ftp/http client, file transfer program supporting a number of network protocols. It is very useful and I've been always using this. There is this case where I need to login on one server through ssh before I will connect to this server and this has helped me a lot.

To connect to a server type :
lftp -u [username],[password] [servername]

Example : lftp -u username,password example.com

Once you are connected, you can do basic commands like "cd" to change directories and "ls" to list files and subdirectories.

To download a file, go to the directory where you want to download the file then connect to the other server using lftp. Go to the directory where the file you want to copy is located then type :
get [filename]

Example : get myfile.txt

To upload a file, go to the directory where the file is located then connect to the other server using lftp. Go to the directory where you want to upload the file then type :
put [filename]

Example : put myfile.txt

Easy isn't it? And it will help you a lot too. Try it out! :)

Tuesday, December 4, 2007

A few linux commands

It's a slow day today. So I'm going to share some very helpful linux commands. These commands are what I always use every time I work since I log in through the server using ssh. This is just an overview of the basic commands. I might discuss later the advance use of each command.

  1. ls - show files and subdirectories under the current directory.

  2. pwd - shows current directory you are

  3. cd - change directory.
    • to go to the parent directory type "cd .." without the quotes
    • to go into a subdirectory, type "cd yoursubdirectoryname" without the quotes

  4. cp - copies a file or a directory.
    • to copy a file, type "cp originaldirectory/originalfile destination/newfile" without the quotes
    • to copy a directory just add -r like "cp -r originaldirectory destination" without the quotes.

  5. rm - removes a file or directory
    • to remove a file, type "rm filename" without the quotes
    • to remove a directory, type "rm -rf direc" without the quotes

  6. vi - opens a powerful text editor. I use this every time.
This is it for now. I will add more later on about connecting to another server using ssh, etc. I hope these are helpful.

Saturday, December 1, 2007

Google Maps Not Working in Internet Explorer, Operation Aborted

This has been bothering me for a while now. This is about the phpGoogleStoreLocator script that I have been working on integrating into a site the past few days. Just when everything was fine and seems to be working properly, it wouldn't function on Internet Explorer. Everytime I open it, it just crashes. I let other people try it and they experience the same thing.

So after googling around I found out that this problem usually occur when you have a script tag inside your tables. This was not the case on my part but I'll be sure to look out for this one. Apparently the solution to my problem is to put the map initialization code into a function say "loadMap()" and attach it to the body tag like this .

Other solutions to the problem if the the ones above still won't work for you are :

  1. Enclosing with a div tag where you want your map to appear. Something like this one : <div id=”map” style=”width: 400px; height: 400px”> and then make sure that all your javascripts are at the bottom of your document below the body tag. Thanks to Ryan.
  2. Moving the map function in the window.onload() handler. Thanks to Justin.
These solution will mostly work but if it still won't try to visit the Microsoft support site here.

Wednesday, November 28, 2007

Slow programming day...been traveling

I just arrived at my parents house today. I came here to visit them until the Christmas season. I've been traveling whole day so no programming for me today. Travel is 5 hours total which consists of 3.5 hours of land travel and 1.5 hours by plane :). I'm going rest for the rest of the day then I will continue programming tomorrow. I'll be back soon to post what I have done.

.... I miss my girlfriend already.. hehehe.

Sunday, November 25, 2007

Exporting mysql data to csv

Today I'm going to discuss the opposite of the previous post. Here it is :

/*some checking here like if the user is logged in, this is important in cases where we don't want other users to get hold of the data*/

//connect to the mysq;
define('host', 'localhost'); //Mysql Server name
define('database', 'dbname'); //Mysql Database name
define('user', 'dbuser'); //Mysql Username
define('password', 'dbpassword'); //Mysql Password
$dbtable = "dbtable";
$connection = mysql_connect(host,user,password);
$db = mysql_select_db(database,$connection);

$sql = mysql_query("select * from ".$dbtable.");

$fields = mysql_list_fields("yourdatabasename",$dbtable);
$columns = mysql_num_fields($fields);
$output = '';

// Put the name of all fields
for ($i = 0; $i < $columns; $i++)
{
$l=mysql_field_name($fields, $i);
$output .= '"'.$l.'",';
}

$output .="\n";

//we now put all values from table
while ($l = mysql_fetch_array($sql))
{
for ($i = 0; $i < $columns; $i++)
{
$output .='"'.$l["$i"].'",';
}

$output .="\n";
}

// Output to browser with appropriate mime type, you choose
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=yourcsvfile.csv");
echo $output;
exit;

?>

This script will prompt for a download for the csvfile named yourcsvfile.csv which contain all the columns and entries from the table dbtable.

Thursday, November 22, 2007

Importing a csv file to mysql

This is one of the things I did with my current project. It is about adding data on MySQL using a csv file and I thought I should share what I did.

First we need to have an html form that will ask for the file input. It will be something like this one:

[form action="import_csv.php" method="post" enctype="multipart/form-data"]
[input name="csv" type="file"]
[input name="filesubmit" value="Submit" type="submit"]
[/form]

Note: Replace "[" and "]" with "<" and ">" respectively. I did it like this because this blog won't allow attaching of html tags.

The enctype attribute is very important because it specifies to use "multipart/form-data" as content type for the form. This also means that the form requires a binary data like the file. The file input type specifies the input is looking for a file. This will appear as a text field with a browse button next to it.

Then we need our php script that will process the csv file and save the entries on the database.

//this is where the file is stored temporarily
$csvfile = $_FILES['csvfile']['tmp_name'];

$file = fopen($csvfile, "r"); //we now open the file
$filesize = filesize($csvfile); //get the filesize

$contents = fread($file, $filesize); //read contents of the file
fclose($file); //close file

$linecount = 0;
$linedata = array();

//you can modify "\n" if your csv file is ended differently
foreach(split("\n",$contents) as $line)
{
$line = trim($line," \t");
$line = str_replace("\r","",$line);
$line = str_replace("'","\'",$line);
$linedata = explode(",",$line);
$mysqlline = implode("','",$linedata);
$linecount++;

//insert the data into the database
//replace $yourtablename with your mysql table name
$sql = mysql_query("insert into '.$yourtablename.' values('$mysqlline')");
}

That's it. We have a simple php script that accepts a csv file and then reads the contents and save the data into your mysql database. Of course this still needs to be optimize. We need to check if the file input is really a csv file. Or is the file empty or too large? I will leave it up to you.

Oh, and be sure that you connect to mysql first. :)

Still raining very hard

This storm just won't stop! Good thing my internet connection is fine now. But I really need to go out to buy some food. I haven't eaten for 8 hours now.

In the meantime, please check my other blog if you're into DOTA (a custom game for warcraft 3). It's my in depth guides and strategies for DOTA.

I hope this rain storm stops soon. I can't work if I'm very hungry. I will post again soon when I've done the things I need to do today( that also means I've already eaten. hehe).