从C锋利客户端应用程序上传到PHP服务器 [英] Upload to PHP server from c sharp client application
问题描述
目前我有一个C锋利的应用程序(客户端应用程序)。和Web应用程序编写PHP。我想,只要在客户端执行特定操作来传输一些文件。下面是客户端代码把文件上传到PHP服务器..
私人无效的button1_Click(对象发件人,EventArgs五)
{
System.Net.WebClient客户端=新System.Net.WebClient();
Client.Headers.Add(内容类型,二进制/八位字节流);
字节[]结果= Client.UploadFile(HTTP://localhost/project1/upload.php,POST,
@C:\test\a。 JPG);
字符串s = System.Text.Encoding.UTF8.GetString(结果为0,result.Length);
}
下面是upload.php的文件,将文件移动..
$ uploads_dir ='./files/'; //目录保存来自客户端应用程序的文件。
的foreach($ _FILES [图片] [错误]为$关键=> $错误){
如果($错误== UPLOAD_ERR_OK){
$ tmp_name的值= $ _FILES [图片] [tmp_name的值] [$关键];
$ NAME = $ _FILES [图片] [名称] [$关键];
move_uploaded_file($ tmp_name的值,$ uploads_dir / $名称);
}
我没有从上面的代码中得到任何错误。但它似乎并不奏效。为什么?我这么想吗?
您当前的PHP代码是处理的多个文件上传,但你的C#代码仅上传有一个文件。
您需要稍微修改你的PHP代码,删除的foreach
循环:
<预类=郎PHP prettyprint-覆盖>
< PHP
$ uploads_dir ='./files'; //目录保存来自客户端应用程序的文件。
如果($ _FILES [文件] [错误] == UPLOAD_ERR_OK){
$ tmp_name的值= $ _FILES [文件] [tmp_name的值];
$ NAME = $ _FILES [文件] [名称];
move_uploaded_file($ tmp_name的值,$ uploads_dir / $名称);
}
>?;
您还需要确保 ./文件
目录存在。
我已经测试过上面的PHP代码与C#代码它完美地工作。
-
有关处理文件上传的更多信息,请参考PHP文档。
-
有关上传的详细信息 多个的 C#和PHP文件,这里有一些有用的链接:
上传与HttpWebRequest的(的multipart / form-data的)文件
如果你想要的东西简单的上传多个文件,你才刚刚上传一个文件来一次
上传。 PHP
在C#循环。
Currently i have a c sharp application (Client app). and a web application written php. I want to transfer some files whenever a particular action is performed at client side. Here is the client code to upload the file to php server..
private void button1_Click(object sender, EventArgs e)
{
System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile("http://localhost/project1/upload.php", "POST",
@"C:\test\a.jpg");
string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
}
Here is the upload.php file to move the file..
$uploads_dir = './files/'; //Directory to save the file that comes from client application.
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
I'm not getting any errors from above code. but it does not seem to be working. Why is it? Am i missing something?
Your current PHP code is for handling multiple file uploads, but your C# code is only uploading one file.
You need to modify your PHP code somewhat, removing the foreach
loop:
<?php
$uploads_dir = './files'; //Directory to save the file that comes from client application.
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>
You also need to ensure that the ./files
directory exists.
I have tested the above PHP code with your C# code and it worked perfectly.
For more information on handling file uploads, refer to the PHP documentation.
For more information on uploading multiple files with C# and PHP, here are some helpful links:
Upload files with HTTPWebrequest (multipart/form-data)
Use Arrays in HTML Form Variables
If you want something simple for uploading multiple files, you just just upload one file at a time to
upload.php
in a C# loop.
这篇关于从C锋利客户端应用程序上传到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!