Thursday, October 11, 2012
Tuesday, October 2, 2012
C#: How to upload files using FTP to a remote server and the localhost
Uploading files to localhost
1. Make your computer a FTP server.
- i use XAMPP to do this.
- Download XAMPP for Windows. Install it.
- Open the XAMPP control panel from the taskbar notification area or C://Xampp folder
OR
- In the Xampp control panel, click the check boxes before Apache, MySql, FileZilla and click on the 3 start buttons.
- Make sure to click "Install" when asked, when you click on the checkbox before FileZilla.
- Now click on the "Admin" button next to "Stop" button of FileZilla. That will open FileZilla Server control panel.
- Select Edit > Users to create a new user.
- Click the "Add" button under "users" and Type a user name and click OK.
- Type a password in Password field.
- Then click on "Shared Folders" in "Page" box.
- Click "Add" button to add the home directory to use as the location to upload files.
- Select the added path and click the "Set as home dir" button to make it your home directory.
- Check all the check boxes Read, Write, Delete etc next to folders list box.
- Click OK of the Users dialog box and close it.
- Ok now you have a FTP server with a username and a pass.
2. Write the C# code.
- Below is the C# code to upload a test.text file as "voot.txt", to the localhost directory. When the code is executed, the "test.txt" file be copied into the localhost home directory as "voot.txt".
// Get the object used to communicate with the server.
//<FOR LOCALHOST>
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/voot.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
//<FOR LOCALHOST>
request.Credentials = new NetworkCredential("kasun", "mypass");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("test.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
Uploading files to a Remote-Host
1. Register for a free/non-free web host with FTP.
- i have used free hosting site "worlditsme.com" for testing.
- Get your Host (ftp) address, username, password from the site after registering.
- Example Host (ftp) address: ftp://yourname.worlditsme.com/
- User name and password will be the ones that you've used to register the worlditsme.com site.
2. Write the C# code.
Below code will upload "test.txt" file as "voot.txt" to the remote hosts, home folder.
// Get the object used to communicate with the server.
//<FOR REMOTE HOST>
//FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://myusername.worlditsme.com/voo.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
//<FOR REMOTE-HOST>
//request.Credentials = new NetworkCredential("kasunl", "mypass");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("test.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
That's it.
Thursday, September 13, 2012
Thursday, August 9, 2012
My Recent Work
Inventory Control System. Programming Language: C#.
(c) GeoSoft.
Kasun Liyanage
geosoft.developers@gmail.com
(c) GeoSoft.
Kasun Liyanage
geosoft.developers@gmail.com
Wednesday, July 25, 2012
Tuesday, June 19, 2012
Power failure destroyed my Win7 boot
Ok so a sudden power failure messed up my Win7 boot configuration, preventing me to log in. I select the 'Start-up Repair' option, but before it get to the repairing the harddisk seemed to stuck in a endless loop doing something. My mind said it could be a disk error, recoverable or otherwise. So i couldn't repair the startup.
But then, as usual, my Linux Ubuntu OS came to the rescue. I switched in to Ubuntu and it said there's a startup problem but not to worry she's fixing it -- like a master. It took a minute an said it fixed it and booted into itself. Now i restarted machine to check Win7, and this time it went directly to the repairing and fixed the boot (took about 10-15 mins to repair) and logged in successfully.
So Linux saved the day, again!
Monday, June 4, 2012
C# Database Programming Start-up Guide. Free E-Book
Saturday, February 18, 2012
PHP simple user Registration + Login system
- First thing, create a MySQl database with the following structure:
- You could create this with the Xamp's PhpMyAdmin utility, or running commands at the php console, or by running commands as a php program.
- I've named the above database as "test", and the table as "bkmark_users".
- Now, we need following 5 files to create the Registration + Login system:
- Login.html: User interface for user registration / login. Includes forms that execute the php codes in CheckLogin.php and Register.php in order to register and login users.
- CheckLogin.php: Search the database for the user inputted username and password to log her/him in. Also creates cookies for storing username and pass.
- Register.php: Writes the user inputted new username and pass into the database as a new user record.
- ProtectedPage.php: This is the page protected by passwords. Firstly it checks whether a user has logged in by checking the cookies. If not, opens the Login page. Also it extracts the username using the cookie to display a message.
- Logout.php: Closes the session deleting the cookies, indicating the user have logged out. Then redirects the user to the Login page.
Ok below are the complete file sources:
Login.html
<html>
<head>
<title>My Website - Login</title>
</head>
<body bgcolor="#FFFFCC">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<table style="margin:15px auto;" align="center">
<tr>
<td colspan="3"><h2>Login:</h2></td>
</tr>
<tr>
<form action="CheckLogin.php" method="post">
<td>Username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="Submit" value="Login"/></td>
</form>
</tr>
</table>
<table style="margin:15px auto;" align="center">
<tr>
<td colspan="3"><h2>Register:</h2></td>
</tr>
<tr>
<form action="Register.php" method="post">
<td>New username:</td>
<td><input type="text" name="newusername"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="newpassword"/></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" name="Submit" value="Register"/></td>
</form>
</tr>
</table>
</html>
CheckLogin.html
<?php
session_start();
$username = $_POST["username"]; // This is the inputted username from the form in Login.html
$password = $_POST["password"]; // This is the inputted password from the form in Login.html
mysql_connect('localhost');
mysql_select_db('test') or die('Could not select database');
$result = mysql_query("SELECT username, password FROM bkmark_users"); // Get data from username and password fields
$hituser=false;
$hitpass=false;
// Sarch username in the database
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
if($row[0] == $username){ // If found, open ProtectedPage.php. Else goto Login.html
$hituser=true;
if($row[1] == $password){
$hitpass=true;
echo "USER FOUND!";
$_SESSION["username"] = $username; // Creates a cookie saving the username
$_SESSION["loggedIn"] = true; // Creates a cookie saying the user is logged in
header("Location:ProtectedPage.php");
}else {
}
}else{
// header("Location:Login.html"); // Lastly, redirect back to login page
}
}
if($hitpass==false){
echo "PASSWORD INCORRECT!<br>";
}
if($hituser==false){
echo "USER NOT FOUND!<br>";
}
?>
Register.html
<?php
session_start();
$newusername = $_POST["newusername"]; // This is the inputted username from the form in Login.html
$newpassword = $_POST["newpassword"]; // This is the inputted password from the form in Login.html
mysql_connect('localhost'); // *** INSERT YOUR DATABSE INFORMATION HERE ***
mysql_select_db('test') or die('Could not select database');
mysql_query("INSERT INTO bkmark_users(username, password) VALUES('" . mysql_real_escape_string($newusername) . "','" . mysql_real_escape_string(
$newpassword) . "')")or die('Could not insert data into db');
echo "New user created:<br>";
echo $newusername."<br>";
echo $newpassword."<br>";
echo "<a href='Logout.php'>Logout</a>";
?>
ProtectedPage.html
<?
session_start();
if(!$_SESSION['loggedIn']) // If the user IS NOT logged in (the loggedIn cookie is created), forward them back to the login page
{
header("location:Login.html");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>My Website - Protected Content</title>
</head>
<body bgcolor="#FFFFCC">
<?php
// If the user IS logged in, then echo the page contents:
$currentUser = $_SESSION['username']; // Gets the username from the cookie we created in Check.php
$message = '<p>Welcome, ' . ucfirst($currentUser) . '!</p>'; // This compiles hello (your username)
echo $message; // This echo's (actually outputs) the message
?>
<p>Good job! You got it working!!</p>
<a href="Logout.php">Logout</a>
</body>
</html>
Logout.html
<?php
session_start();
session_destroy();
header("Location:Login.html");
?>
Create these files inside the same folder and run the Login.html file. Enjoy!
Create these files inside the same folder and run the Login.html file. Enjoy!
Wednesday, February 15, 2012
PHP and MySql. How to connect to a database, Create a table, and add data
Below code will,
1. Make a sql connection to localhost (display error if something goes wrong).
2. Select the database "test" as the working db.
3. Create a table named example2 inside the "test" db.
4. Set the member variables and their data types in the table.
5. Insert 3 rows of data into the table.
6. Retrieve back the data from the table as a resource ($result).
7. Use the resource to print out the contents inside the table, with a while loop.
=============================================================
// 1. Make a MySQL Connection
mysql_connect("localhost") or die(mysql_error());
// 2.
mysql_select_db("test") or die(mysql_error());
// 3. Create a MySQL table in the selected database
mysql_query("CREATE TABLE example2(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
age INT)")
or die(mysql_error());
// 5. Insert rows of information into the table "example2"
mysql_query("INSERT INTO example2
(name, age) VALUES('Timmy Mellowman', '23' ) ")
or die(mysql_error());
mysql_query("INSERT INTO example2
(name, age) VALUES('Sandy Smith', '21' ) ")
or die(mysql_error());
mysql_query("INSERT INTO example2
(name, age) VALUES('Bobby Wallace', '15' ) ")
or die(mysql_error());
// 6. Retrieve all the data from the "example2" table
$result = mysql_query("SELECT * FROM example2")
or die(mysql_error());
// 7. store the record of the "example2" table into $row
// Print out the contents of the entry
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['age'];
echo "
";
}
?>
=============================================================
(( SQL help ))
SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
SQL INSERT INTO Syntax
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
SELECT * Example
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
The mysql_query() function
Executes a query on a MySQL database.
The mysql_fetch_array() function
Returns a row from a recordset
Subscribe to:
Posts (Atom)
-
S o you wanna learn the basics of C# database programming? Then read along this booklet, and you’ll learn how to build your own data...























