One challenge that comes up a few times a year for various clients is moving data from a legacy system to a web application. For instance if your billing system or ERP system creates a report that you want to display to your customers online, you may want to send that report to the webapp.

In this case Acme Corp. has a list of product inventory that they want to upload daily from a Windows 2003 server to their handy PHP app online. Let’s write them two applications. One to send the list to the server and one to accept it on the server. We aren’t going to do any fancy processing, that’s a topic for another time, but were at least going to get that file.

The Server

First we need to write a PHP application to accept the file. We are going to somewhat secure about it and at least require a plain text username and password. You can use better security, but this will do. If it is going over the Internet, I’d also suggest you use HTTPS to transport the data


/* Modify these values */
define(USERNAME, "myuser");
define(PASSWORD, "mypass");
define(MYAPP, "Upload101");
define(MYDEST, "data-".date("Ymd").".csv");

if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="'.MYAPP.'"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'You must login to send '.MYAPP.' data.';
} else {
			/* PUT data comes in on the stdin stream */
			$putdata = fopen("php://input", "r");
			/* Open a file for writing */
			$fp = fopen(MYDEST, "w");
			/* Read the data 1 KB at a time
			   and write to the file */
			while ($data = fread($putdata, 1024))
			  fwrite($fp, $data);
			/* Close the streams */
			foreach($_FILES as $name=>$val)
				move_uploaded_file($val['tmp_name'], MYDEST);
				chmod($dest, 0664);
	    header('WWW-Authenticate: Basic realm="'.MYAPP.'"');
	    header('HTTP/1.0 401 Unauthorized');
	    echo 'Invalid credentials';

The application above supports both PUT and POST style uploads. VB.Net will generally use the multi-part POST method, but it is good to support both. Make sure to modify the constants at the top of the script based on your needs. Then upload it to your webserver.

The Client

Next write your VB.Net Application, which will login to the web server and send the file.

Imports System.Net

Module MyUpload

    Sub Main(ByVal args As String())
        Dim argCount As Integer = 0
        Dim username, password, filename, url As String

        For Each arg As String In args
            If (argCount = 0) Then username = arg
            If (argCount = 1) Then password = arg
            If (argCount = 2) Then filename = arg
            If (argCount = 3) Then url = arg
            argCount = argCount + 1

        If (argCount = 4) Then
            Dim instance As WebClient = New WebClient
            instance.Credentials = New NetworkCredential(username, password)
            instance.UploadFile(url, filename)
            Console.WriteLine("Syntax: MyUpload.exe [username] [password] [filename] [url]")
        End If
    End Sub

End Module

You can download a copy of the MyUpload application:
MyUpload.exe [12kb, MD5 Checksum: 0b2c875e8d05864dd9c81d04624d46ea]

Putting It All Together

You then run MyUpload.exe with the parameters you set in your PHP application for username and password and the URL of your upload script. Note the double backslashes \\ in the filename.

MyUpload.exe myuser mypass c:\\myfile.csv

You can then add it as a scheduled task to automate the process.