<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2787259437228203206</id><updated>2011-04-21T15:46:49.117-07:00</updated><category term='linux'/><category term='perl'/><title type='text'>Rohit D'souza</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-3184240075038283404</id><published>2008-05-22T04:07:00.000-07:00</published><updated>2008-05-22T04:25:46.843-07:00</updated><title type='text'>Coding ftp in perl</title><content type='html'>So how do we code ftp in perl.Its easy to get started read on....&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Lets see what are the main methods for ftp.First of all one should have the Net:FTP package to use ftp in perl.&lt;br /&gt;Below are the list and explanation of some important ftp class methods which will get us started.&lt;br /&gt;&lt;br /&gt;CONSTRUCTOR&lt;br /&gt;&lt;br /&gt;    * new ([ HOST ] [, OPTIONS ])&lt;br /&gt;&lt;br /&gt;      This is the constructor for a new Net::FTP object. HOST is the name of the remote host to which an FTP connection is required.&lt;br /&gt;&lt;br /&gt;      HOST is optional. If HOST is not given then it may instead be passed as the Host option described below.&lt;br /&gt;&lt;br /&gt;      OPTIONS are passed in a hash like fashion, using key and value pairs. Possible options are:&lt;br /&gt;&lt;br /&gt;      Host - FTP host to connect to. It may be a single scalar, as defined for the PeerAddr option in IO::Socket::INET, or a reference to an array with hosts to try in turn. The "host" method will return the value which was used to connect to the host.&lt;br /&gt;&lt;br /&gt;      Firewall - The name of a machine which acts as an FTP firewall. This can be overridden by an environment variable FTP_FIREWALL . If specified, and the given host cannot be directly connected to, then the connection is made to the firewall machine and the string @hostname is appended to the login identifier. This kind of setup is also referred to as an ftp proxy.&lt;br /&gt;&lt;br /&gt;      FirewallType - The type of firewall running on the machine indicated by Firewall. This can be overridden by an environment variable FTP_FIREWALL_TYPE . For a list of permissible types, see the description of ftp_firewall_type in Net::Config.&lt;br /&gt;&lt;br /&gt;      BlockSize - This is the block size that Net::FTP will use when doing transfers. (defaults to 10240)&lt;br /&gt;&lt;br /&gt;      Port - The port number to connect to on the remote machine for the FTP connection&lt;br /&gt;&lt;br /&gt;      Timeout - Set a timeout value (defaults to 120)&lt;br /&gt;&lt;br /&gt;      Debug - debug level (see the debug method in Net::Cmd)&lt;br /&gt;&lt;br /&gt;      Passive - If set to a non-zero value then all data transfers will be done using passive mode. If set to zero then data transfers will be done using active mode. If the machine is connected to the Internet directly, both passive and active mode should work equally well. Behind most firewall and NAT configurations passive mode has a better chance of working. However, in some rare firewall configurations, active mode actually works when passive mode doesn't. Some really old FTP servers might not implement passive transfers. If not specified, then the transfer mode is set by the environment variable FTP_PASSIVE or if that one is not set by the settings done by the libnetcfg utility. If none of these apply then passive mode is used.&lt;br /&gt;&lt;br /&gt;      Hash - If given a reference to a file handle (e.g., \*STDERR ), print hash marks (#) on that filehandle every 1024 bytes. This simply invokes the hash() method for you, so that hash marks are displayed for all transfers. You can, of course, call hash() explicitly whenever you'd like.&lt;br /&gt;&lt;br /&gt;      LocalAddr - Local address to use for all socket connections, this argument will be passed to IO::Socket::INET&lt;br /&gt;&lt;br /&gt;      If the constructor fails undef will be returned and an error message will be in $@&lt;br /&gt;&lt;br /&gt;METHODS&lt;br /&gt;&lt;br /&gt;Unless otherwise stated all methods return either a true or false value, with true meaning that the operation was a success. When a method states that it returns a value, failure will be returned as undef or an empty list.&lt;br /&gt;&lt;br /&gt;    * login ([LOGIN [,PASSWORD [, ACCOUNT] ] ])&lt;br /&gt;&lt;br /&gt;      Log into the remote FTP server with the given login information. If no arguments are given then the Net::FTP uses the Net::Netrc package to lookup the login information for the connected host. If no information is found then a login of anonymous is used. If no password is given and the login is anonymous then anonymous@ will be used for password.&lt;br /&gt;&lt;br /&gt;      If the connection is via a firewall then the authorize method will be called with no arguments.&lt;br /&gt;    * authorize ( [AUTH [, RESP]])&lt;br /&gt;&lt;br /&gt;      This is a protocol used by some firewall ftp proxies. It is used to authorise the user to send data out. If both arguments are not specified then authorize uses Net::Netrc to do a lookup.&lt;br /&gt;    * site (ARGS)&lt;br /&gt;&lt;br /&gt;      Send a SITE command to the remote server and wait for a response.&lt;br /&gt;&lt;br /&gt;      Returns most significant digit of the response code.&lt;br /&gt;    * ascii&lt;br /&gt;&lt;br /&gt;      Transfer file in ASCII. CRLF translation will be done if required&lt;br /&gt;    * binary&lt;br /&gt;&lt;br /&gt;      Transfer file in binary mode. No transformation will be done.&lt;br /&gt;&lt;br /&gt;      Hint: If both server and client machines use the same line ending for text files, then it will be faster to transfer all files in binary mode.&lt;br /&gt;    * rename ( OLDNAME, NEWNAME )&lt;br /&gt;&lt;br /&gt;      Rename a file on the remote FTP server from OLDNAME to NEWNAME . This is done by sending the RNFR and RNTO commands.&lt;br /&gt;    * delete ( FILENAME )&lt;br /&gt;&lt;br /&gt;      Send a request to the server to delete FILENAME .&lt;br /&gt;    * cwd ( [ DIR ] )&lt;br /&gt;&lt;br /&gt;      Attempt to change directory to the directory given in $dir . If $dir is ".." , the FTP CDUP command is used to attempt to move up one directory. If no directory is given then an attempt is made to change the directory to the root directory.&lt;br /&gt;    * cdup ()&lt;br /&gt;&lt;br /&gt;      Change directory to the parent of the current directory.&lt;br /&gt;    * pwd ()&lt;br /&gt;&lt;br /&gt;      Returns the full pathname of the current directory.&lt;br /&gt;    * restart ( WHERE )&lt;br /&gt;&lt;br /&gt;      Set the byte offset at which to begin the next data transfer. Net::FTP simply records this value and uses it when during the next data transfer. For this reason this method will not return an error, but setting it may cause a subsequent data transfer to fail.&lt;br /&gt;    * rmdir ( DIR [, RECURSE ])&lt;br /&gt;&lt;br /&gt;      Remove the directory with the name DIR . If RECURSE is true then rmdir will attempt to delete everything inside the directory.&lt;br /&gt;    * mkdir ( DIR [, RECURSE ])&lt;br /&gt;&lt;br /&gt;      Create a new directory with the name DIR . If RECURSE is true then mkdir will attempt to create all the directories in the given path.&lt;br /&gt;&lt;br /&gt;      Returns the full pathname to the new directory.&lt;br /&gt;    * alloc ( SIZE [, RECORD_SIZE] )&lt;br /&gt;&lt;br /&gt;      The alloc command allows you to give the ftp server a hint about the size of the file about to be transferred using the ALLO ftp command. Some storage systems use this to make intelligent decisions about how to store the file. The SIZE argument represents the size of the file in bytes. The RECORD_SIZE argument indicates a maximum record or page size for files sent with a record or page structure.&lt;br /&gt;&lt;br /&gt;      The size of the file will be determined, and sent to the server automatically for normal files so that this method need only be called if you are transferring data from a socket, named pipe, or other stream not associated with a normal file.&lt;br /&gt;    * ls ( [ DIR ] )&lt;br /&gt;&lt;br /&gt;      Get a directory listing of DIR , or the current directory.&lt;br /&gt;&lt;br /&gt;      In an array context, returns a list of lines returned from the server. In a scalar context, returns a reference to a list.&lt;br /&gt;    * dir ( [ DIR ] )&lt;br /&gt;&lt;br /&gt;      Get a directory listing of DIR , or the current directory in long format.&lt;br /&gt;&lt;br /&gt;      In an array context, returns a list of lines returned from the server. In a scalar context, returns a reference to a list.&lt;br /&gt;    * get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )&lt;br /&gt;&lt;br /&gt;      Get REMOTE_FILE from the server and store locally. LOCAL_FILE may be a filename or a filehandle. If not specified, the file will be stored in the current directory with the same leafname as the remote file.&lt;br /&gt;&lt;br /&gt;      If WHERE is given then the first WHERE bytes of the file will not be transferred, and the remaining bytes will be appended to the local file if it already exists.&lt;br /&gt;&lt;br /&gt;      Returns LOCAL_FILE , or the generated local file name if LOCAL_FILE is not given. If an error was encountered undef is returned.&lt;br /&gt;    * put ( LOCAL_FILE [, REMOTE_FILE ] )&lt;br /&gt;&lt;br /&gt;      Put a file on the remote server. LOCAL_FILE may be a name or a filehandle. If LOCAL_FILE is a filehandle then REMOTE_FILE must be specified. If REMOTE_FILE is not specified then the file will be stored in the current directory with the same leafname as LOCAL_FILE .&lt;br /&gt;&lt;br /&gt;      Returns REMOTE_FILE , or the generated remote filename if REMOTE_FILE is not given.&lt;br /&gt;&lt;br /&gt;      NOTE: If for some reason the transfer does not complete and an error is returned then the contents that had been transferred will not be remove automatically.&lt;br /&gt;    * put_unique ( LOCAL_FILE [, REMOTE_FILE ] )&lt;br /&gt;&lt;br /&gt;      Same as put but uses the STOU command.&lt;br /&gt;&lt;br /&gt;      Returns the name of the file on the server.&lt;br /&gt;    * append ( LOCAL_FILE [, REMOTE_FILE ] )&lt;br /&gt;&lt;br /&gt;      Same as put but appends to the file on the remote server.&lt;br /&gt;&lt;br /&gt;      Returns REMOTE_FILE , or the generated remote filename if REMOTE_FILE is not given.&lt;br /&gt;    * unique_name ()&lt;br /&gt;&lt;br /&gt;      Returns the name of the last file stored on the server using the STOU command.&lt;br /&gt;    * mdtm ( FILE )&lt;br /&gt;&lt;br /&gt;      Returns the modification time of the given file&lt;br /&gt;    * size ( FILE )&lt;br /&gt;&lt;br /&gt;      Returns the size in bytes for the given file as stored on the remote server.&lt;br /&gt;&lt;br /&gt;      NOTE: The size reported is the size of the stored file on the remote server. If the file is subsequently transferred from the server in ASCII mode and the remote server and local machine have different ideas about "End Of Line" then the size of file on the local machine after transfer may be different.&lt;br /&gt;    * supported ( CMD )&lt;br /&gt;&lt;br /&gt;      Returns TRUE if the remote server supports the given command.&lt;br /&gt;    * hash ( [FILEHANDLE_GLOB_REF],[ BYTES_PER_HASH_MARK] )&lt;br /&gt;&lt;br /&gt;      Called without parameters, or with the first argument false, hash marks are suppressed. If the first argument is true but not a reference to a file handle glob, then \*STDERR is used. The second argument is the number of bytes per hash mark printed, and defaults to 1024. In all cases the return value is a reference to an array of two: the filehandle glob reference and the bytes per hash mark.&lt;br /&gt;    * feature ( NAME )&lt;br /&gt;&lt;br /&gt;      Determine if the server supports the specified feature. The return value is a list of lines the server responded with to describe the options that it supports for the given feature. If the feature is unsupported then the empty list is returned.&lt;br /&gt;&lt;br /&gt;        if ($ftp-&gt;feature( 'MDTM' )) {&lt;br /&gt;          # Do something&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if (grep { /\bTLS\b/ } $ftp-&gt;feature('AUTH')) {&lt;br /&gt;          # Server supports TLS&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;The following methods can return different results depending on how they are called. If the user explicitly calls either of the pasv or port methods then these methods will return a true or false value. If the user does not call either of these methods then the result will be a reference to a Net::FTP::dataconn based object.&lt;br /&gt;&lt;br /&gt;    * nlst ( [ DIR ] )&lt;br /&gt;&lt;br /&gt;      Send an NLST command to the server, with an optional parameter.&lt;br /&gt;    * list ( [ DIR ] )&lt;br /&gt;&lt;br /&gt;      Same as nlst but using the LIST command&lt;br /&gt;    * retr ( FILE )&lt;br /&gt;&lt;br /&gt;      Begin the retrieval of a file called FILE from the remote server.&lt;br /&gt;    * stor ( FILE )&lt;br /&gt;&lt;br /&gt;      Tell the server that you wish to store a file. FILE is the name of the new file that should be created.&lt;br /&gt;    * stou ( FILE )&lt;br /&gt;&lt;br /&gt;      Same as stor but using the STOU command. The name of the unique file which was created on the server will be available via the unique_name method after the data connection has been closed.&lt;br /&gt;    * appe ( FILE )&lt;br /&gt;&lt;br /&gt;      Tell the server that we want to append some data to the end of a file called FILE . If this file does not exist then create it.&lt;br /&gt;&lt;br /&gt;If for some reason you want to have complete control over the data connection, this includes generating it and calling the response method when required, then the user can use these methods to do so.&lt;br /&gt;&lt;br /&gt;However calling these methods only affects the use of the methods above that can return a data connection. They have no effect on methods get , put , put_unique and those that do not require data connections.&lt;br /&gt;&lt;br /&gt;    * port ( [ PORT ] )&lt;br /&gt;&lt;br /&gt;      Send a PORT command to the server. If PORT is specified then it is sent to the server. If not, then a listen socket is created and the correct information sent to the server.&lt;br /&gt;    * pasv ()&lt;br /&gt;&lt;br /&gt;      Tell the server to go into passive mode. Returns the text that represents the port on which the server is listening, this text is in a suitable form to sent to another ftp server using the port method.&lt;br /&gt;&lt;br /&gt;The following methods can be used to transfer files between two remote servers, providing that these two servers can connect directly to each other.&lt;br /&gt;&lt;br /&gt;    * pasv_xfer ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )&lt;br /&gt;&lt;br /&gt;      This method will do a file transfer between two remote ftp servers. If DEST_FILE is omitted then the leaf name of SRC_FILE will be used.&lt;br /&gt;    * pasv_xfer_unique ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )&lt;br /&gt;&lt;br /&gt;      Like pasv_xfer but the file is stored on the remote server using the STOU command.&lt;br /&gt;    * pasv_wait ( NON_PASV_SERVER )&lt;br /&gt;&lt;br /&gt;      This method can be used to wait for a transfer to complete between a passive server and a non-passive server. The method should be called on the passive server with the Net::FTP object for the non-passive server passed as an argument.&lt;br /&gt;    * abort ()&lt;br /&gt;&lt;br /&gt;      Abort the current data transfer.&lt;br /&gt;    * quit ()&lt;br /&gt;&lt;br /&gt;      Send the QUIT command to the remote FTP server and close the socket connection.&lt;br /&gt;&lt;br /&gt;Methods for the adventurous&lt;br /&gt;&lt;br /&gt;Net::FTP inherits from Net::Cmd so methods defined in Net::Cmd may be used to send commands to the remote FTP server.&lt;br /&gt;&lt;br /&gt;    * quot (CMD [,ARGS])&lt;br /&gt;&lt;br /&gt;      Send a command, that Net::FTP does not directly support, to the remote server and wait for a response.&lt;br /&gt;&lt;br /&gt;      Returns most significant digit of the response code.&lt;br /&gt;&lt;br /&gt;      WARNING This call should only be used on commands that do not require data connections. Misuse of this method can hang the connection.&lt;br /&gt;&lt;br /&gt;THE dataconn CLASS&lt;br /&gt;&lt;br /&gt;Some of the methods defined in Net::FTP return an object which will be derived from this class.The dataconn class itself is derived from the IO::Socket::INET class, so any normal IO operations can be performed. However the following methods are defined in the dataconn class and IO should be performed using these.&lt;br /&gt;&lt;br /&gt;    * read ( BUFFER, SIZE [, TIMEOUT ] )&lt;br /&gt;&lt;br /&gt;      Read SIZE bytes of data from the server and place it into BUFFER , also performing any &lt;CRLF&gt; translation necessary. TIMEOUT is optional, if not given, the timeout value from the command connection will be used.&lt;br /&gt;&lt;br /&gt;      Returns the number of bytes read before any &lt;CRLF&gt; translation.&lt;br /&gt;    * write ( BUFFER, SIZE [, TIMEOUT ] )&lt;br /&gt;&lt;br /&gt;      Write SIZE bytes of data from BUFFER to the server, also performing any &lt;CRLF&gt; translation necessary. TIMEOUT is optional, if not given, the timeout value from the command connection will be used.&lt;br /&gt;&lt;br /&gt;      Returns the number of bytes written before any &lt;CRLF&gt; translation.&lt;br /&gt;    * bytes_read ()&lt;br /&gt;&lt;br /&gt;      Returns the number of bytes read so far.&lt;br /&gt;    * abort ()&lt;br /&gt;&lt;br /&gt;      Abort the current data transfer.&lt;br /&gt;    * close ()&lt;br /&gt;&lt;br /&gt;      Close the data connection and get a response from the FTP server. Returns true if the connection was closed successfully and the first digit of the response from the server was a '2'.&lt;br /&gt;&lt;br /&gt;UNIMPLEMENTED&lt;br /&gt;&lt;br /&gt;The following RFC959 commands have not been implemented:&lt;br /&gt;&lt;br /&gt;    * SMNT&lt;br /&gt;&lt;br /&gt;      Mount a different file system structure without changing login or accounting information.&lt;br /&gt;    * HELP&lt;br /&gt;&lt;br /&gt;      Ask the server for "helpful information" (that's what the RFC says) on the commands it accepts.&lt;br /&gt;    * MODE&lt;br /&gt;&lt;br /&gt;      Specifies transfer mode (stream, block or compressed) for file to be transferred.&lt;br /&gt;    * SYST&lt;br /&gt;&lt;br /&gt;      Request remote server system identification.&lt;br /&gt;    * STAT&lt;br /&gt;&lt;br /&gt;      Request remote server status.&lt;br /&gt;    * STRU&lt;br /&gt;&lt;br /&gt;      Specifies file structure for file to be transferred.&lt;br /&gt;    * REIN&lt;br /&gt;&lt;br /&gt;      Reinitialize the connection, flushing all I/O and account information.&lt;br /&gt;Now lets look at a simple example&lt;br /&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;use Net::FTP;##########Define the ftp class&lt;br /&gt;&lt;br /&gt;###########this is error printing subroutine###################&lt;br /&gt;sub gettheerror {&lt;br /&gt;  print "Error in your ftp commands are: \n";&lt;br /&gt;  print @ERRORS;&lt;br /&gt;  exit 0;&lt;br /&gt;} &lt;br /&gt;#first we have to define the ftp hostname and the directory to browse&lt;br /&gt;my $ftphost="yourftpsite.com";&lt;br /&gt;my $browsedirectory="rohitdsouza";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$ftp=Net::FTP-&gt;new($ftphost,Timeout=&gt;350) or $gettheerror=1;#####this connects to the host or throws an error&lt;br /&gt;  push @ERRORS, "Can't ftp to $host: $!\n" if $gettheerror;#######we push the arrays onto a array which we will display in the error display routine&lt;br /&gt;  $gettheerror() if $gettheerror;&lt;br /&gt;print "Connected to te ftp host $ftphost\n";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$ftp-&gt;login("rohitdsouza","password") or $gettheerror=1;########now login to the account&lt;br /&gt;#If no information is found then a login of anonymous is used. If no password is given and the login is anonymous then anonymous@ will be used for password.&lt;br /&gt;&lt;br /&gt;#If the connection is via a firewall then the authorize  method will be called with no arguments.&lt;br /&gt;&lt;br /&gt;print "Getting file list";&lt;br /&gt;  push @ERRORS, "Can't login to $host: $!\n" if $gettheerror;&lt;br /&gt;  $ftp-&gt;quit if $gettheerror;&lt;br /&gt;  gettheerror() if $gettheerror; &lt;br /&gt;print "Logged in\n";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$ftp-&gt;cwd($browsedirectory) or $gettheerror=1; now chnage the current working directory to the desired directory&lt;br /&gt;  push @ERRORS, "Can't cd  $!\n" if $gettheerror;&lt;br /&gt;  gettheerror() if $gettheerror;&lt;br /&gt;  $ftp-&gt;quit if $gettheerror;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;@files=$ftp-&gt;dir or $gettheerror=1;# do the file listing by puuting the file names in an array&lt;br /&gt;  push @ERRORS, "Can't get file list $!\n" if $gettheerror;&lt;br /&gt;  gettheerror() if $gettheerror;&lt;br /&gt;print "Got  file list\n";   &lt;br /&gt;foreach(@files) {&lt;br /&gt;  print "$_\n";#######print the files here&lt;br /&gt;  }&lt;br /&gt;$ftp-&gt;quit;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-3184240075038283404?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/3184240075038283404/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=3184240075038283404' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/3184240075038283404'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/3184240075038283404'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2008/05/coding-ftp-in-perl.html' title='Coding ftp in perl'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-3569010590040653901</id><published>2008-05-19T23:10:00.000-07:00</published><updated>2008-05-19T23:38:25.855-07:00</updated><title type='text'>How backup and restore mysql databases</title><content type='html'>I thought to write about this topic as there are many developers around who want to take a dump of mysql database from the live server to the local testing server.&lt;br /&gt;&lt;br /&gt;They then search the web if they dont know how to do it.This article is obviously dedicated to those people.&lt;br /&gt;&lt;br /&gt;so lets get started..........&lt;br /&gt;&lt;br /&gt;first step take a dump of the whole mysql database using below command on CLI&lt;br /&gt;&lt;br /&gt;mysqldump –-user [user name] –-password=[password] [database name] &gt; [dump file]&lt;br /&gt;&lt;br /&gt;Let's take a look at each of the arguments that can be passed to the mysqldump utility, as shown above:&lt;br /&gt;&lt;br /&gt;    * --user [user name]: The --user flag followed by a valid MySQL username tells MySQL the username of the account that we want to use to perform the database dump. MySQL user accounts are stored in the "user" table of the "mysql" database. You can view a list of users and their permissions for your MySQL server by using the following code at the MySQL console application:&lt;br /&gt;&lt;br /&gt;      use mysql;&lt;br /&gt;&lt;br /&gt;      select * from user;&lt;br /&gt;    * --password=[password]: The password for the user account mentioned above.&lt;br /&gt;    * [database name]: The name of the database that we would like the mysqldump utility to backup. Instead of specifying one single database name, we could use either --databases or --all-databases to backup every single database on our MySQL server.&lt;br /&gt;    * &gt; [dump file]: If you're familiar with DOS and batch files, then you will know that the "&gt;" symbol specifies that we are directing output to a stream, port, or file. For the mysqldump utility, we prepend a "&gt;" to the filename we would like our database to be backed up to. If no path is specified for the file, then it will be created in the current directory.&lt;br /&gt;&lt;br /&gt;Lets take a example&lt;br /&gt;&lt;br /&gt;lets say i want to back up a database named rohitdata on my live server into rohitdata.txt for subsequent import to my local server.&lt;br /&gt;&lt;br /&gt;The syntax for this is as follows &lt;br /&gt;&lt;br /&gt;mysqldump –-user='rohitdsouza' –-password='rohitdsouza' rohitdata &gt; rohitdata.txt&lt;br /&gt;&lt;br /&gt;Only catch here is one should create a database first if it doesn't exists before executing the above command.&lt;br /&gt;&lt;br /&gt;for that us create database rohitdata;&lt;br /&gt;&lt;br /&gt;Ok after we execute this command successfully and we have our dump file its just a matter of one more command on the CLI on our local server to restore the database&lt;br /&gt;&lt;br /&gt;mysql --user='rohit' --password='rohit' rohitdata &lt; rohitdata.txt&lt;br /&gt;&lt;br /&gt;the dump file should be in the current directory if no path is specified.&lt;br /&gt;&lt;br /&gt;Thats all.Keep rocking people.&lt;br /&gt;also check my cool blog www.rohitdsouza.blogspot.com&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-3569010590040653901?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/3569010590040653901/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=3569010590040653901' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/3569010590040653901'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/3569010590040653901'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2008/05/how-backup-and-restore-mysql-databases.html' title='How backup and restore mysql databases'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-1424169289461201680</id><published>2008-05-17T22:06:00.000-07:00</published><updated>2008-05-17T22:21:36.537-07:00</updated><title type='text'>Generating unique id in php</title><content type='html'>Hey people.&lt;br /&gt;&lt;br /&gt;I had gone to a interview some years back and the interviewer had asked me a question about how does one generate an uuid in php and at that time i never used or knew there was something like uniqid function in php as i used to generate a uuid myself combining various unique parameters from a web request.Then that guy told me he has a good alternative to my code and it takes less lines of codes.I now use this function often as it suits my purpose.&lt;br /&gt;&lt;br /&gt;So lets see how we can generate a uuid in php .&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$getmy_uuid=md5(uniqid(rand(),true));&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Thats all isn't that a kewl single line of code.To know what the uniqid parameters are&lt;br /&gt;read this article http://in.php.net/manual/en/function.uniqid.php.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-1424169289461201680?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/1424169289461201680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=1424169289461201680' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/1424169289461201680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/1424169289461201680'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2008/05/generating-unique-id-in-php.html' title='Generating unique id in php'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-5408732059040636469</id><published>2008-05-17T22:02:00.000-07:00</published><updated>2008-05-17T22:24:21.247-07:00</updated><title type='text'>Regular expressions in php</title><content type='html'>Hey folks,&lt;br /&gt;&lt;br /&gt;Sorry for not posting articles regularly on this blog.Anyways i am gonna take you through a kinda crash course in regular expressions in php.&lt;br /&gt;&lt;br /&gt;I am really found of regular expressions as they help me to accomplish many tasks like scraping data from the web,matching patterns in config file on my Linux box etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;so lets get started.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The first set of functions one can use for pattern matching are ereg functions.&lt;br /&gt;&lt;br /&gt;The ereg functionsfor pattern matching requires one to specify the regular expression as a string,for example ereg('regexpression', "subject") checks if regexpression matches subject string. One should use a single quoted string when passing a regular expression as a literal string. We do this as several special characters like the dollar and backslash are also special characters in double-quoted PHP strings, but not in single-quoted PHP strings.&lt;br /&gt;Remember friends double quoted string in scripting languages like php /perl interpolate the variables if present any inside the string.&lt;br /&gt;&lt;br /&gt;int ereg (string pattern, string subject [, array groups]) returns the length of the match if the regular expression pattern matches the subject string or part of the subject string, or zero otherwise. Since zero evaluates to False and non-zero evaluates to True, you can use ereg in an if statement to test for a match. If you specify the third parameter, ereg will store the substring matched by the part of the regular expression between the first pair of  round brackets in $groups[1]. $groups[2] will contain the second pair, and so on. Note that grouping-only round brackets are not supported by ereg. ereg is case sensitive. eregi is the case insensitive equivalent.&lt;br /&gt;string ereg_replace (string pattern, string replacement, string subject) replaces all matches of the regex patten in the subject string with the replacement string. You can use backrefrences in the replacement string. \\0 is the entire regex match, \\1 is the first backreference, \\2 the second, etc. The highest possible backreference is \\9. ereg_replace is case sensitive. eregi_replace is the case insensitive equivalent.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;?&lt;br /&gt;if(ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})",&lt;br /&gt;$_SERVER['REMOTE_ADDR'],$regs))&lt;br /&gt; {&lt;br /&gt;    echo "Last 2 octets of the remote ip are:  "."$regs[3].$regs[4]";&lt;br /&gt;}&lt;br /&gt;else{&lt;br /&gt;&lt;br /&gt;    echo "Invalid remote ip ".$_SERVER['REMOTE_ADDR'];&lt;br /&gt;}&lt;br /&gt;$string = "24/6/1982";&lt;br /&gt;echo ereg_replace("([0-9]+)/([0-9]+)/([0-9]+)",&lt;br /&gt; "\\3-\\2-\\1", $string);&lt;br /&gt;///changing date to mysql date format&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But people I advice all the developers to use preg_match as it is faster than ereg.&lt;br /&gt;&lt;br /&gt;So now let’s look at preg functions. They are kewl and the main advantage they have when they are pitted against the ereg functions is faster execution time.&lt;br /&gt;&lt;br /&gt;All of the preg functions require you to specify the regular expression as a string using Perl syntax. In Perl use of /regexpression/ defines a regular expression while in PHP, this becomes preg_match('/regexpression/', $subject).Remember all forward slashes in the regular expression have to be escaped with a backslash as they signify the delimitation of the regular expression to match.&lt;br /&gt;To specify regexpression matching options such as case insensitivity are specified in the same way as in Perl. '/regexpression/i' applies the regexpression case insensitively. '/regexpression/s' makes the dot match all characters. '/regexpression/m' makes the start and end of line anchors match at embedded newlines in the subject string. '/regexpression/x' turns on free spacing mode. You can specify multiple letters to turn on several options. '/regexpression/misx' turns on all four options.&lt;br /&gt;A special option is the /u which turns on the unicode matching mode, instead of the default 8-bit matching mode. You should specify /u for regular expressions that use \x{FFFF}, \X or \p{L} to match Unicode characters, graphemes, properties or scripts. PHP will interpret '/regexpression/u' as a UTF-8 string rather than as an ASCII string.&lt;br /&gt;Like the ereg function, bool preg_match (string pattern, string subject [, array groups]) returns TRUE if the regular expression pattern matches the subject string or part of the subject string. If you specify the third parameter, preg will store the substring matched by the first capturing group in $groups[1]. $groups[2] will contain the second pair, and so on. If the regex pattern uses named capture, you can access the groups by name with $groups['name']. $groups[0] will hold the overall match.&lt;br /&gt;int preg_match_all (string pattern, string subject, array matches, int flags) fills the array "matches" with all the matches of the regular expression pattern in the subject string. If you specify PREG_SET_ORDER as the flag, then $matches[0] is an array containing the match and backreferences of the first match, just like the $groups array filled by preg_match. $matches[1] holds the results for the second match, and so on. If you specify PREG_PATTERN_ORDER, then $matches[0] is an array with full subsequent regex matches, $matches[1] an array with the first backreference of all matches, $matches[2] an array with the second backreference of each match, etc.&lt;br /&gt;array preg_grep (string pattern, array subjects) returns an array that contains all the strings in the array "subjects" that can be matched by the regular expression pattern.&lt;br /&gt;mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit]) returns a string with all matches of the regex pattern in the subject string replaced with the replacement string. At most limit replacements are made. One key difference is that all parameters, except limit, can be arrays instead of strings. In that case, preg_replace does its job multiple times, iterating over the elements in the arrays simultaneously. You can also use strings for some parameters, and arrays for others. Then the function will iterate over the arrays, and use the same strings for each iteration. Using an array of the pattern and replacement, allows you to perform a sequence of search and replace operations on a single subject string. Using an array for the subject string, allows you to perform the same search and replace operation on many subject strings.&lt;br /&gt;preg_replace_callback (mixed pattern, callback replacement, mixed subject [, int limit]) works just like preg_replace, except that the second parameter takes a callback instead of a string or an array of strings. The callback function will be called for each match. The callback should accept a single parameter. This parameter will be an array of strings, with element 0 holding the overall regex match, and the other elements the text matched by capturing groups. This is the same array you'd get from preg_match. The callback function should return the text that the match should be replaced with. Return an empty string to delete the match. Return $groups[0] to skip this match.&lt;br /&gt;array preg_split (string pattern, string subject [, int limit]) works just like split, except that it uses the Perl syntax for the regex pattern.&lt;br /&gt;&lt;br /&gt;Lets look at some examples now.&lt;br /&gt;&lt;br /&gt;Example:-&lt;br /&gt;&lt;br /&gt;&lt;?&lt;br /&gt;if(preg_match('/([a-zA-Z\s]+) scored (\d+) out of&lt;br /&gt; (\d+) in ([a-zA-Z]+)/','Rohit Dsouza scored 97 out of 100 in  &lt;br /&gt;maths',$matches)){&lt;br /&gt;&lt;br /&gt;#print "The Student named ".$matches[1]." scored ".$matches[2]."/".&lt;br /&gt;$matches[3]." in ".$matches[4]."\n";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;else{&lt;br /&gt;&lt;br /&gt;print "match not found";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if(preg_match_all('/([a-zA-Z\s]+) scored (\d+) out of (\d+) in&lt;br /&gt; ([a-zA-Z]+)/','Rohit Dsouza scored 97 out of 100 in maths John  &lt;br /&gt;Dsouza scored 95 out of 100 in Physics',$matches)){&lt;br /&gt;#print_r($matches);&lt;br /&gt;#print "Count:".count($matches[0]);&lt;br /&gt;&lt;br /&gt;for($c=0;$c&lt;count($matches[0]);$c++){&lt;br /&gt;&lt;br /&gt;print "The Student named ".$matches[1][$c]." scored ".$matches[2][$c]."/".$matches[3][$c]." in ".$matches[4][$c]."\n";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;else{&lt;br /&gt;&lt;br /&gt;print "match not found";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;print_r(preg_grep('/([a-zA-Z\s]+) scored (\d+) out of (\d+) in ([a-zA-Z]+)/',array('Rohit Dsouza scored 97 out of 100 in maths','John Dsouza scored 95 out of 100 in Physics')));&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$string='rohit dsouza is smart';&lt;br /&gt;print preg_replace('/rohit dsouza/','rajeev nair',$string);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$string='Rohit Dsouza scored 97 out of 100 in maths john Dsouza scored 93 out of 100 in physics';&lt;br /&gt;&lt;br /&gt;echo preg_replace_callback ('/([a-zA-Z\s]+) scored (\d+) &lt;br /&gt;out of (\d+) in ([a-zA-Z]+)/','call_replacement',$string);&lt;br /&gt;function call_replacement($matches){&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;###################replace and return code here#####################&lt;br /&gt;return "$matches[1] is a genius";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;#print $string;&lt;br /&gt;$split_array=preg_split('/\s/','rohit dsouza');&lt;br /&gt;print "My First name is ".$split_array[0]." and my Surname is ".$split_array[1];&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Thats all.if you guys have any query the do mail me at rajdsouza at yahoo dot com&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-5408732059040636469?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/5408732059040636469/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=5408732059040636469' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/5408732059040636469'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/5408732059040636469'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2008/05/regular-expressions-in-php.html' title='Regular expressions in php'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-7731396101010074203</id><published>2008-01-16T03:41:00.000-08:00</published><updated>2008-01-16T04:01:44.289-08:00</updated><title type='text'>How to Start writing Web Crawler in perl</title><content type='html'>Hey People,&lt;br /&gt;&lt;br /&gt;Crawlers are kewl.i always thought writing crawlers is a tough job.Until i had to write one.I am posting a small code snippet of how to get started to write ur own crawler in perl.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The packages which u will require is &lt;br /&gt;HTML::TreeBuilder&lt;br /&gt;LWP::UserAgent&lt;br /&gt;HTTP::Headers&lt;br /&gt;URI::Escape&lt;br /&gt;&lt;br /&gt;Here how to start up.&lt;br /&gt;&lt;br /&gt;if u want to crawl results for search text "rohit d'souza" then u start up with google as below.&lt;br /&gt;&lt;br /&gt;$searchtext=uri_escape("rohit d'souza")&lt;br /&gt;$ua = LWP::UserAgent-&gt;new;#ur user agent &lt;br /&gt; $ua-&gt;agent("Mozilla/5.0");&lt;br /&gt;         $ua-&gt;timeout(3000000);&lt;br /&gt;  # Create a request&lt;br /&gt;     my $req = HTTP::Request-&gt;new(GET =&gt; "http://www.google.co.in/search?hl=en&amp;q=$searchtext&amp;btnG=Google+Search&amp;meta=&lt;br /&gt;     ");&lt;br /&gt; my $res = $ua-&gt;request($req);&lt;br /&gt;&lt;br /&gt; if ($res-&gt;is_success) {&lt;br /&gt;$tree = HTML::TreeBuilder-&gt;new_from_content($res-&gt;content);&lt;br /&gt;if(defined $tree-&gt;look_down( '_tag' =&gt; 'a' )){&lt;br /&gt;@getlinks=$tree-&gt;look_down( '_tag' =&gt; 'a' );&lt;br /&gt;&lt;br /&gt;for($b=0;$b&lt;@getlinks;$b++){&lt;br /&gt;&lt;br /&gt;        if($getlinks[$b]-&gt;attr('href') and $getlinks[$b]-&gt;attr('href')!~/google|orkut|^\/(.*)/gi ){&lt;br /&gt;        push(@links,$getlinks[$b]-&gt;attr('href'));&lt;br /&gt;#####################################################################&lt;br /&gt;Do repeated processing here to crawl other pages from the links extracted&lt;br /&gt;#####################################################################&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;print "got failure".$res-&gt;status_line;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Thats all u can build a full fledge crawler using this login.Mind u the above code provided is a simple one.i suggest to build a professional crawler u need to build a OOPS structure and use functions to call repeatedly called code snippet.&lt;br /&gt;&lt;br /&gt;If u have any queries u can mail me at &lt;span style="font-weight:bold;"&gt;topindiancoder@gmail.com&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;One who never fails never grows an inch&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-7731396101010074203?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/7731396101010074203/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=7731396101010074203' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/7731396101010074203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/7731396101010074203'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2008/01/how-to-start-writing-web-crawler-in.html' title='How to Start writing Web Crawler in perl'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-4264807888788323918</id><published>2007-11-06T04:32:00.000-08:00</published><updated>2007-11-18T23:42:27.383-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><title type='text'>Connecting to Sqlserver from linux platform</title><content type='html'>Phew this was really tough job for me.But i accomplised it after much hardships.Thanks to my sysadmin who had no knowledge about it and i did his job.&lt;br /&gt;&lt;br /&gt;Below are the stepd to get this job done&lt;br /&gt;&lt;br /&gt;first an d for all u require unixodbc installed on ur pc.This can be found at http://www.unixodbc.org/&lt;br /&gt;&lt;br /&gt;download it and get it installed&lt;br /&gt;&lt;br /&gt;then download freetds from www.freetds.org&lt;br /&gt;&lt;br /&gt;and install it as below&lt;br /&gt;./configure  --prefix=/usr/local/freetds --sysconfdir=/usr/local/ freetds --with-tdsver=7.0  --with-unixodbc=/usr/local &lt;br /&gt;&lt;br /&gt;make&lt;br /&gt;&lt;br /&gt;make install&lt;br /&gt;&lt;br /&gt;then open vim /usr/local/freetds/freetds.conf&lt;br /&gt;then put ur pc details where u have sql server installed in that file&lt;br /&gt;[rohit2k]&lt;br /&gt;host = 192.156.78.9&lt;br /&gt;port =1433&lt;br /&gt;tds version = 7.0&lt;br /&gt;here [rohit2k] is the pc name&lt;br /&gt;&lt;br /&gt;then test the connection by executing the below command &lt;br /&gt;&lt;br /&gt;tsql –S ‘rohit2k’ –U ‘sa’ –P ‘password’&lt;br /&gt;&lt;br /&gt;if u see a 1&gt; then ur connection is done.&lt;br /&gt;&lt;br /&gt;The FreeTDS ODBC driver should be installed in /usr/local/freetds/lib - look for libtdsodbc.so.&lt;br /&gt;&lt;br /&gt;Configuration&lt;br /&gt;&lt;br /&gt;Registering the ODBC Driver With unixODBC&lt;br /&gt;&lt;br /&gt;unixODBC needs to know about all ODBC drivers you intend to use. The best way to accomplished this is using the ODBCConfig graphical program which comes with unixODBC. An alternative method is to use the 'odbcinst' command which also comes with unixODBC. We will focus on using the odbcinst command. Create a file named tds.driver.template with a few lines describing the driver.&lt;br /&gt;[FreeTDS]&lt;br /&gt;Description     = v0.63 with protocol v8.0&lt;br /&gt;Driver          = /usr/local/freetds/lib/libtdsodbc.so&lt;br /&gt;tds.driver.template&lt;br /&gt;&lt;br /&gt;Execute odbcinst, telling it to install a driver entry using the tds.driver.template file. Note that you must leave a space between the '-f' switch and the template file name.&lt;br /&gt;&lt;br /&gt;# odbcinst -i -d -f tds.driver.template&lt;br /&gt; &lt;br /&gt;register ODBC driver&lt;br /&gt;&lt;br /&gt;Creating an ODBC Data Source Name&lt;br /&gt;&lt;br /&gt;ODBC client applications will typically work with ODBC Data Source Names (DSN). The best way to create, edit and remove a DSN is to use the ODBCConfig tool. Again we will use the odbcinst command instead. We will do this because the ODBCConfig program is fairly self explanatory and because not all users will have the unixODBC GUI tools installed. Again, we start by creating a template file - this one is called tds.datasource.template and contains some options such as the default database and UID. Note; you will want to use your own Server address.&lt;br /&gt;[MSSQLTestServer]&lt;br /&gt;Driver  = FreeTDS&lt;br /&gt;Description     = Northwind sample database&lt;br /&gt;Trace   = No&lt;br /&gt;Server      = 192.168.1.25&lt;br /&gt;Port      = 1433&lt;br /&gt;Database        = Northwind&lt;br /&gt;tds.datasource.template&lt;br /&gt;&lt;br /&gt;Valid attributes for use in odbc.ini (or the connection string) can be found at: http://www.freetds.org/userguide/odbcconnattr.htm&lt;br /&gt;[SybaseTestServer]&lt;br /&gt;Driver  = FreeTDS&lt;br /&gt;Description     = Test Sybase Database with FreeTDS&lt;br /&gt;Trace   = No&lt;br /&gt;Server      = 192.168.1.25&lt;br /&gt;Port      = 5050&lt;br /&gt;TDS Version      = 5.0&lt;br /&gt;Database        = testdb&lt;br /&gt;tds.datasource.template&lt;br /&gt;&lt;br /&gt;Note: Do NOT use "Servername" in an ODBC-only configuration! Use of "Server" should be preferred over "Address" (see http://lists.ibiblio.org/pipermail/freetds/2004q2/016086.html)&lt;br /&gt;&lt;br /&gt;Note; we have executed previous commands as root (denoted by leading '#' character on given commands) but here we execute the command as a regular user. This is significant. All users of the system share FreeTDS and the ODBC Drivers but each user has his/her own list of DSN's (view odbcinst output for help on registering as a system DSN available to all users). So create the DSN as the user who is going to be using it.&lt;br /&gt;&lt;br /&gt;$ odbcinst -i -s -f tds.datasource.template&lt;br /&gt; &lt;br /&gt;create ODBC data source&lt;br /&gt;&lt;br /&gt;Now, to make this work with PHP is very easy. Assuming you have a package manager, download and install the odbc extension for PHP if it isn't already installed. If you don't have a package manager, you will have to compile odbc.so. To enable odbc in PHP, just modify the php.ini file, usually located in /etc. Add this line anywhere in the file (convention dictates that it should be placed with the other extension calls, so search your file for the word "extension").&lt;br /&gt;&lt;br /&gt;extension = odbc.so&lt;br /&gt; &lt;br /&gt;php.ini&lt;br /&gt;&lt;br /&gt;Now, reload apache as root to make the changes effective.&lt;br /&gt;&lt;br /&gt;# apachectl graceful&lt;br /&gt; &lt;br /&gt;reloading apache as root&lt;br /&gt;&lt;br /&gt;Test&lt;br /&gt;&lt;br /&gt;unixODBC comes with a variety of tools which allow you to test. We will use the command line tool 'isql'. isql allows us to submit commands (typically SQL statements) to the DSN and see the results.&lt;br /&gt;&lt;br /&gt;$ isql -v MSSQLTestServer&lt;br /&gt;SQL&gt;&lt;br /&gt; &lt;br /&gt;test using isql&lt;br /&gt;You should see a connected message and an SQL prompt. If this fails then you may have a configuration problem or you may simply be using the incorrect UserName and PWD.&lt;br /&gt;&lt;br /&gt;Now try a simple SQL statement.&lt;br /&gt;&lt;br /&gt;SQL&gt; SELECT contactname FROM customers&lt;br /&gt; &lt;br /&gt;simple sql statement&lt;br /&gt;&lt;br /&gt;hey its finally over.hope ur serach for this problem ends here&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-4264807888788323918?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/4264807888788323918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=4264807888788323918' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/4264807888788323918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/4264807888788323918'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2007/11/connecting-to-sqlserver-from-linux.html' title='Connecting to Sqlserver from linux platform'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2787259437228203206.post-4895927258368473442</id><published>2007-11-05T02:05:00.000-08:00</published><updated>2007-11-05T21:42:54.813-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='perl'/><title type='text'>Perl file upload</title><content type='html'>hey guys&lt;br /&gt;&lt;br /&gt;it has been long since i have posted any code online.Got a bit busy with my work @ my office.But people i am back  and wanted to do something even  better this time .so i thought i should open a blog where all my codes can reside.i have done lot of file uploading in php and then i bumped into a project where i was required to do file uploading in perl.Long back my freind was trying to do the same and he told me that its tough to do so.but i never looked into it at that time and now i started writing the code for the same with a small fear in mind(Thanks to my freind) that the code will take a toll on my grey cells.but it didnot.Its so simple.&lt;br /&gt;Here it goes&lt;br /&gt;&lt;br /&gt;here is the html code for file upload&lt;br /&gt;&amp;lt;form action="upload.cgi" method="post" enctype="multipart/form-data" onsubmit="return checkfile();"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;table align="center" border="0" cellpadding="0" cellspacing="0"&amp;gt;&lt;br /&gt;&amp;lt;tbody&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td align="center"&amp;gt;&amp;lt;b&amp;gt;File upload by rohit d'souza&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&amp;lt;td align="center"&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&amp;lt;td align="center"&amp;gt;&lt;br /&gt;File to Upload: &amp;lt;input name="filerohit" type="file"&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;input name="Submit" value="Submit Form" type="submit"&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/tbody&amp;gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&lt;br /&gt;here checkfile is a javascript function to check if the user has actaully selected a file before submitting the form.&lt;br /&gt;&lt;br /&gt;function checkfile()&lt;br /&gt; {&lt;br /&gt;&lt;br /&gt; if(document.forms[0].filerohit.value=="")&lt;br /&gt; {&lt;br /&gt;     alert("please select a file to upload");&lt;br /&gt;     return false;&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;here is the server side code in upload.cgi&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;use CGI;&lt;br /&gt;use strict;&lt;br /&gt;my $query = new CGI;&lt;br /&gt;my $upload_dir="/opt/myuploads";&lt;br /&gt;my $filename = $query-&gt;param("filerohit");&lt;br /&gt;$filename =~ s/.*[\/\\](.*)/$1/;&lt;br /&gt;my $upload_filehandle = $query-&gt;upload("filerohit");&lt;br /&gt;&lt;br /&gt;open UPLOADFILE, "&gt;$upload_dir/$filename";&lt;br /&gt;&lt;br /&gt;binmode UPLOADFILE;&lt;br /&gt;&lt;br /&gt;while ( &lt;$upload_filehandle&gt; )&lt;br /&gt;{&lt;br /&gt;print UPLOADFILE;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;close UPLOADFILE;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;and now u can do anything u want with the file which is at the location $upload_dir/$filename&lt;br /&gt;&lt;br /&gt;Thats all.hope this code helps u.u could send me ur feedbacks at rajdsouza@yahoo.com&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2787259437228203206-4895927258368473442?l=indiascoder.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://indiascoder.blogspot.com/feeds/4895927258368473442/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2787259437228203206&amp;postID=4895927258368473442' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/4895927258368473442'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2787259437228203206/posts/default/4895927258368473442'/><link rel='alternate' type='text/html' href='http://indiascoder.blogspot.com/2007/11/perl-file-upload.html' title='Perl file upload'/><author><name>Black Knight</name><uri>http://www.blogger.com/profile/13636622936413522980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
