Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual Basic-将文件上载到PHP网页_Php_Vb.net_Visual Studio 2010 - Fatal编程技术网

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

Visual Basic-将文件上载到PHP网页,php,vb.net,visual-studio-2010,Php,Vb.net,Visual Studio 2010,我正在尝试将一个图像文件上载到web服务器上的PHP文件 在VB.NET上-> My.Computer.Network.UploadFile(tempImageLocation, "website.com/upload.php") tempImageLocation是硬盘上映像所在的位置。图像位于我指定的硬盘驱动器上 关于PHP-> $image = $_FILES['uploads']['name']; 我不明白,因为它正在加载页面-但PHP在“上载”下找不到文件。这里有一个快速而肮脏的教

我正在尝试将一个图像文件上载到web服务器上的PHP文件

在VB.NET上->

My.Computer.Network.UploadFile(tempImageLocation, "website.com/upload.php")
tempImageLocation是硬盘上映像所在的位置。图像位于我指定的硬盘驱动器上

关于PHP->

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

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

这里有一个快速而肮脏的教程供您使用:

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


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

如果不设置字段名,则可以使用此字段保存上载的文件

$file = array_shift($_FILES);
move_uploaded_file($file['tmp_name'], '/path/to/new/location/'.$file['name']);
请看一些答案。PHP要求使用POST方法上载的文件,这些文件通常在从web表单上载时由浏览器设置,但可以在VB中使用设置


至于PHP端,在使用
$image=$\u文件['uploads']['name']上传后,您将无法立即找到该文件。PHP使用一个临时文件名存储上传,该文件名可通过
$\u FILES['uploads']['tmp\u name']
变量访问,使用
move\u uploadd\u file()
是将上传从临时存储转移到永久上传目录的标准方法。在我搜索同一个问题时,谷歌把我带到了这里。感谢大家,它给了我这个想法,我对PHP有了一点了解,就实现了。我知道这是一个老问题,但我还是要和大家分享我的代码,这样将来它可以帮助人们

VB:

PHP:


不要忘了给上传文件夹适当的权限。

下面是使用Visual Basic上传文件的完整示例,在服务器端PHP(Rest API)

我知道,它已经过时了。。但我有一个解决方案:

    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=header&vbNewLine&“内容长度:”&sizeInBytes.ToString&vbNewLine header=header&vbNewLine&“Expect:100 continue”&vbNewLine


希望得到帮助。

这是我的示例服务器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']);

?>

哈哈。有趣的是,之所以需要通过Visual Basic上传,是因为我的应用程序正在制作图像,需要上传,而且需要无缝上传。我不希望我的用户必须找到图像本身才能上传它!:太感谢你了!虽然我不再需要它了,但我非常感谢你花时间帮助我回答这个问题。
HttpUploadFile("https://www.yousite.com/ws/upload.php?option1=sss&options2=12121", FULL_FILE_NAME_PATH_IN_YOUR_PC, "files", "multipart/form-data")
<?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']);

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