大家好我正在制作一个可视化的基本应用程序,用于将文件和文件夹与webDAV服务器同步,但是我在服务器上传/创建目录时遇到了问题,但没有上传文件,这是我用来尝试创建目录的代码在服务器上。
Private Sub FldrSync()
PictureBox1.Location = New Point(12, 161)
PictureBox1.Size = New Point(756, 147)
PictureBox1.Visible = True
Dim folderDlg As New FolderBrowserDialog
folderDlg.SelectedPath = TextBox1.Text
TextBox1.Text = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
For Each foundFile As String In Directory.GetDirectories(folderDlg.SelectedPath)
ListBox1.Items.Add(foundFile)
Dim check As String = _
System.IO.Path.GetFileName(foundFile)
Dim fileToUpload As String = foundFile
Dim fileLength As Long = 0
Dim url As String = TextBox2.Text
Dim port As String = ""
'If the port was provided, then insert it into the url.
If port <> "" Then
'Insert the port into the Url
'https://www.example.com:80/directory
Dim u As New Uri(url)
'Get the host (example: "www.example.com")
Dim host As String = u.Host
'Replace the host with the host:port
url = url.Replace(host, host & ":" & port)
End If
url = url.TrimEnd("/"c) & "/" & IO.Path.GetFileName(fileToUpload)
ListBox1.Items.Add(url)
Dim userName As String = TextBox4.Text
Dim password As String = TextBox3.Text
'Create the request
Dim request As HttpWebRequest = _
DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
'Set the User Name and Password
request.Credentials = New NetworkCredential(userName, password)
'Let the server know we want to "put" a file on it
request.Method = WebRequestMethods.Http.Put
'Set the length of the content (file) we are sending
request.ContentLength = fileLength
'*** This is required for our WebDav server ***
request.SendChunked = True
request.Headers.Add("Translate: f")
request.AllowWriteStreamBuffering = True
'Send the request to the server, and get the
' server's (file) Stream in return.
Dim s As IO.Stream = request.GetRequestStream()
'Open the file so we can read the data from it
'Create the buffer for storing the bytes read from the file
Dim byteTransferRate As Integer = 1024
Dim bytes(byteTransferRate - 1) As Byte
Dim bytesRead As Integer = 0
Dim totalBytesRead As Long = 0
'Read from the file and write it to the server's stream.
Do
'Read from the file
bytesRead = 0
If bytesRead > 0 Then
totalBytesRead += bytesRead
'Write to stream
s.Write(bytes, 0, bytesRead)
End If
Loop While bytesRead > 0
'Close the server stream
s.Close()
s.Dispose()
s = Nothing
'Close the file
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
'Get the StatusCode from the server's Response
Dim code As HttpStatusCode = response.StatusCode
'Close the response
response.Close()
response = Nothing
'Validate the uploaded file.
' Check the totalBytesRead and the fileLength: Both must be an exact match.
'
' Check the StatusCode from the server and make sure the file was "Created"
' Note: There are many different possible status codes. You can choose
' which ones you want to test for by looking at the "HttpStatusCode" enumerator.
If totalBytesRead = fileLength AndAlso _
code = HttpStatusCode.Created Then
MessageBox.Show("The file has uploaded successfully!", "Upload Complete", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("The file did not upload successfully.", _
"Upload Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Next
PictureBox1.Location = New Point(494, 5)
PictureBox1.Size = New Point(28, 20)
PictureBox1.Visible = False
End Sub
我找到了一种将文件夹上传到webDAV服务器的方法,但我使用的是root权限,因为webDAV文件夹运行在ownCloud服务器上,这意味着用户文件的权限设置为nobody:nobody。但是文件夹会上传,但用户无法编辑,删除或创建文件。这个代码如下。
Visual Basic
Private Sub CreateFolders()
PictureBox1.Location = New Point(12, 161)
PictureBox1.Size = New Point(756, 147)
PictureBox1.Visible = True
Dim folderDlg As New FolderBrowserDialog
folderDlg.SelectedPath = TextBox1.Text
TextBox1.Text = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
For Each foundFile As String In Directory.GetFiles(folderDlg.SelectedPath)
ListBox1.Items.Add(foundFile)
Dim check As String = _
System.IO.Path.GetFileName(foundFile)
WebBrowser2.Navigate("http://myserver/BOXSYNC/foldercreate.php?localfolder=" & TextBox1.Text & "&boxfolder=" & TextBox2.Text & "&username=" & TextBox4.Text & "&password=" & TextBox3.Text & "&filename=" & check & "&foldername=" & FldrName.Text)
ListBox1.Items.Add(check)
Next
PictureBox1.Location = New Point(494, 5)
PictureBox1.Size = New Point(28, 20)
PictureBox1.Visible = False
End Sub
PHP
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
include('Net/SFTP.php');
?>
<?php
$local = $_GET['localfolder'];
echo $local;
echo "<br>";
$box = $_GET['boxfolder'];
echo $box;
echo "<br>";
$box = str_replace("https://box.com/remote.php/webdav/","",$box);
echo $box;
echo "<br>";
$file = $_GET['filename'];
echo $file;
echo "<br>";
$user = $_GET['username'];
echo $user;
echo "<br>";
$sftp = new Net_SFTP('the.hostingsite.com');
if (!$sftp->login('root', 'password')) {
exit('Login Failed');
}
$media_path = '/home/box/public_html/data/'.$user.'/files/'.$box.'/'.$file;
$origin_path = '/home/box/public_html/data/'.$user.'/files/'.$box;
$sftp->mkdir($media_path);
echo $media_path;
echo "<br>";
$sftp->chown($media_path, 'nobody');
echo "<br>";
$sftp->chmod($media_path, 0755);
$sftp->chown($media_path, 'nobody', true);
$sftp->chgrp($media_path, 'nobody', true);
$sftp->chdir($media_path);
echo "<br>";
$file_name= $file;
$path = "/home/sites/php.net/public_html/sandbox/" . $file_name ;
$user_name = "box82";
chown($path, $user_name);
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));
?>
<form method="post" action="index.php" id="syncform" style="display:none;">
<input type="text" readonly="readonly" id="localfolder" value="<?php echo $local; ?>" />
<input type="text" readonly="readonly" id="boxfolder" value="<?php echo $box; ?>" />
<input type="text" readonly="readonly" id="username" value="<?php echo $user; ?>" />
<input type="text" readonly="readonly" id="password" value="" />
<input type="text" readonly="readonly" id="filename" value="<?php echo $file; ?>" />
<input type="text" readonly="readonly" id="foldername" value="" />
<input type="submit" id="sync" value="Sync" />
</form>
如果你能帮助我那会很棒! 谢谢。