Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:esta.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
485 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...