0

我正在尝试将图像文件上传到 Web 服务器上的 PHP 文件。

在 VB.NET 上 ->

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

tempImageLocation 是图像所在的硬盘驱动器上的位置。该图像位于我指定它的硬盘驱动器上。

在 PHP 上 ->

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

我不明白,因为它正在加载页面 - 但 PHP 在“上传”下找不到文件

4

7 回答 7

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"]);

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

于 2013-12-01T07:43:16.230 回答
2

我知道,是旧的.. 但这里是我的解决方案工作:

    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")

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

header = header & vbNewLine & "Content-Length: " & sizeInBytes.ToString & vbNewLine header = header & vbNewLine & "Expect: 100-continue" & vbNewLine

希望有所帮助。

于 2019-10-06T21:25:25.703 回答
1

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

于 2015-10-15T13:05:18.120 回答
0

这是给你的快速而肮脏的教程:PHP文件上传

“上传”只是表单元素的名称属性值:

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

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

于 2012-05-29T03:43:24.810 回答
0

如果不设置字段名,可以用这个保存上传的文件

$file = array_shift($_FILES);
move_uploaded_file($file['tmp_name'], '/path/to/new/location/'.$file['name']);
于 2012-05-29T04:01:35.287 回答
0

看看其他一些 答案。PHP 要求使用 POST 方法上传的文件使用某些标题,这些标题通常在从 Web 表单上传时由浏览器设置,但可以在 VB 中使用HttpWebRequest 类进行设置。

至于 PHP 方面,您将无法在使用$image = $_FILES['uploads']['name'];. $_FILES['uploads']['tmp_name']PHP 使用可通过变量访问的临时文件名存储上传,使用move_uploaded_file()是将上传从临时存储转移到永久上传目录的标准方法。PHP 手册对此进行了很好的概述。

于 2012-05-29T04:05:21.837 回答
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")
于 2020-04-14T04:31:50.340 回答