HTTPWebRequest to php upload

Master Zero

Well-known member
Joined
Sep 10, 2005
Messages
51
Programming Experience
Beginner
I have a php scripts that allows upload to my web site. I was wondering if it is possible to upload to the script using HTTPWebRequest? I prefer using a web stream so that I can show the progress. I have paste the script below because no matter how many examples I try, they just don’t seem to work.

VB.NET:
<?php
if (!empty($_FILES['uploaded_file']))
{
	move_uploaded_file($_FILES['uploaded_file']['tmp_name'],
		"../../Control/Upload/".$_FILES['uploaded_file']['name']);
	
	echo "<h2><u>File information</u></h2>";
	echo "<b>Name:</b> ", $_FILES['uploaded_file']['name'], "<br />";
	echo "<b>Type (MIME):</b> ", $_FILES['uploaded_file']['type'], "<br />";
	echo "<b>Size (bytes):</b> ", $_FILES['uploaded_file']['size'], "<br />";
	echo "<b>Location:</b> ", $_FILES['uploaded_file']['tmp_name'], "<br />";
	echo "<b>Error Code:</b> ", $_FILES['uploaded_file']['error'];
}
else
{
	echo "<form enctype='multipart/form-data' action='http://www.FAKESITE.com/php/Upload/index.php' method='post'> <br />";
	echo "<input type='hidden' name='MAX_FILE_SIZE' value='' /> <br />";
	echo "Choose a file to upload: <input type='file' name='uploaded_file' />";
	echo "<input type='submit' value='Upload' /> <br />";
	echo "</form>";
}
?>
 
If you change the file input name to "file" (and script $_FILES['file']) you can use the My.Computer.Network.UploadFile method which also supports progress and cancellation.

Else you need a lot of code to manually construct the POST message like this:
VB.NET:
Dim filepath As String = IO.Path.Combine(Application.StartupPath, "filename.ext")
Dim url As String = "http://www.FAKESITE.com/php/Upload/index.php"

Dim boundary As String = IO.Path.GetRandomFileName
Dim header As New System.Text.StringBuilder()
header.AppendLine("--" & boundary)
header.Append("Content-Disposition: form-data; name=""uploaded_file"";")
header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filepath))
header.AppendLine()
header.AppendLine("Content-Type: application/octet-stream")
header.AppendLine()

Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)

Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = headerbytes.Length + New IO.FileInfo(filepath).Length + endboundarybytes.Length
req.Method = "POST"

Dim s As IO.Stream = req.GetRequestStream
s.Write(headerbytes, 0, headerbytes.Length)
Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)
s.Write(filebytes, 0, filebytes.Length)
s.Write(endboundarybytes, 0, endboundarybytes.Length)
s.Close()
 
Thank you so much for your help. It works perfectly!
 
I need help! I need to upload a file from vb to the php webserver. How? Do you have any example? How can i contact you?
 
Last edited by a moderator:
Back
Top