Uploading files to a web server using Visual Basic.
Do you need to upload a file to your php web server from a .NET application? I did, and I found that it was actually pretty easy. Basically the upload.php file works the same way as a basic HTML website form. You simply do an HTML form post from within your .NET application.
The source code for the VB.NET application:
Dim WC As New System.Net.WebClient
Dim uriString As String = “http://www.yourwebserver.com/upload.php”
Dim fileName As String = “c:\somefile.txt”
Dim responseArray As Byte() = WC.UploadFile(uriString, “POST”, fileName)
MsgBox(System.Text.Encoding.ASCII.GetString(responseArray))
The source code for the PHP upload.php script:
<?php
$uploaddir = "/home/webspace/htdocs/uploads/";
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)){
echo "File Uploaded!";
}else{
echo "Problem uploading file.";
}
?>
Call the .NET code above to upload a file. Make sure you have a writable directory on your php web server. The application should throw a message box up with the results of upload.php. ( Basically whatever upload.php echo’s back. )
