Visual Basic - 将文件上传到PHP网页

时间:2012-05-29 03:35:40

标签: php vb.net visual-studio-2010

我正在尝试将图片文件上传到网络服务器上的PHP文件。

在VB.NET上 - >

My.Computer.Network.UploadFile(tempImageLocation, "website.com/upload.php")

tempImageLocation是图像所在硬盘上的一个位置。图像 位于我指定的硬盘上。

在PHP上 - >

$image = $_FILES['uploads']['name'];

我不明白,因为它正在加载页面 - 但是PHP无法在'uploads'下找到该文件

7 个答案:

答案 0 :(得分:5)

当我在寻找同样的问题时谷歌把我带到了这里。感谢大家,它给了我这个想法,并且凭借对PHP的一点了解,我已经实现了它。我知道这是一个老问题,但我仍然要分享我的代码,这样它可以在将来帮助人们。

VB:

My.Computer.Network.UploadFile("e:\file1.jpg", "http://www.mysite.com/upl/upl.php")

PHP:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

并且不要忘记为上传文件夹提供适当的权限。

答案 1 :(得分:1)

以下是使用 Visual Basic 和服务器端上传文件的完整示例 PHP(Rest API) GitHub Link

答案 2 :(得分:1)

我知道,已经老了..但这是我的解决方案:

    Private Sub HttpUploadFile(
ByVal uri As String,
ByVal filePath As String,
ByVal fileParameterName As String,
ByVal contentType As String)

    Dim myFile As New FileInfo(filePath)
    Dim sizeInBytes As Long = myFile.Length

    Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
    Dim newLine As String = System.Environment.NewLine
    Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
    Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
    request.ContentType = "multipart/form-data; boundary=" & boundary
    request.Method = "POST"
    request.KeepAlive = True
    'request.Credentials = Net.CredentialCache.DefaultCredentials

    Using requestStream As IO.Stream = request.GetRequestStream()
        Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}"
        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)

        Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3};"
        Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType)
        header = header & vbNewLine & "Content-Length: " & sizeInBytes.ToString & vbNewLine
        header = header & "Expect: 100-continue" & vbNewLine & vbNewLine

        'MsgBox(header)
        Debug.Print(header)

        Dim headerBytes As Byte() = Encoding.UTF8.GetBytes(header)
        requestStream.Write(headerBytes, 0, header.Length)

        Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim buffer(4096) As Byte
            Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
            Do While (bytesRead > 0)
                requestStream.Write(buffer, 0, bytesRead)
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)
            Loop
        End Using
        Dim trailer As Byte() = Encoding.ASCII.GetBytes(newLine & "--" + boundary + "--" & newLine)
        requestStream.Write(trailer, 0, trailer.Length)
        requestStream.Close()
    End Using


    Dim response As Net.WebResponse = Nothing
    Try
        response = request.GetResponse()
        Using responseStream As IO.Stream = response.GetResponseStream()
            Using responseReader As New IO.StreamReader(responseStream)
                Dim responseText = responseReader.ReadToEnd()
                Debug.Print(responseText)
            End Using
        End Using
    Catch exception As Net.WebException
        response = exception.Response
        If (response IsNot Nothing) Then
            Using reader As New IO.StreamReader(response.GetResponseStream())
                Dim responseText = reader.ReadToEnd()
                Diagnostics.Debug.Write(responseText)
            End Using
            response.Close()
        End If
    Finally
        request = Nothing
    End Try
End Sub

使用:

HttpUploadFile("https://www.yousite.com/ws/upload.php?option1=sss&options2=12121", FULL_FILE_NAME_PATH_IN_YOUR_PC, "files", "multipart/form-data")

我在不记得的网站上复制了代码。 我只使用了这两行代码:

header =标头&vbNewLine&“ Content-Length:”&sizeInBytes.ToString&vbNewLine 标头=标头&vbNewLine&“期望:100-继续”&vbNewLine

希望获得帮助。

答案 3 :(得分:0)

以下是快速而又脏的教程:PHP File Upload

'uploads'只是表单元素的名称属性值:

<input type="file" name="uploads" />

或换句话说,这是通过$ _FILES global访问的POST变量名。

答案 4 :(得分:0)

如果您未设置字段名称,则可以使用此

保存上传的文件
$file = array_shift($_FILES);
move_uploaded_file($file['tmp_name'], '/path/to/new/location/'.$file['name']);

答案 5 :(得分:0)

查看一些these other个答案。 PHP要求使用POST方法上传的文件使用certain headers,这些文件通常由浏览器在从Web表单上传时设置,但可以使用HttpWebRequest Class在VB中设置。

对于PHP方面,在使用$image = $_FILES['uploads']['name'];上传后,您将无法立即找到该文件。 PHP使用$_FILES['uploads']['tmp_name']变量访问临时文件名来存储上传,使用move_uploaded_file()是将上传从临时存储转移到永久上传目录的标准方法。 PHP manual提供了很好的概述。

答案 6 :(得分:0)

这是我的示例服务器php文件:

<?php
// write to a log file so you know it's working
$msg = $_POST['w'];
$logfile= 'data.txt';

$fp = fopen($logfile, "a");
fwrite($fp, $msg);
fclose($fp);

$file = array_shift($_FILES);
move_uploaded_file($file['tmp_name'], '/MAMP/htdocs/test/'.$file['name']);

?>

以下是调用@Rodrigo的代码的代码:

HttpUploadFile("http://localhost/test/test.php?w=hello&options2=12121", "C:\temp\bahamas.mp3", "files", "multipart/form-data")