18

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Fawzan Izy
  • 1,780
  • 3
  • 14
  • 16

2 Answers2

17

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.

Community
  • 1
  • 1
Xenon
  • 3,174
  • 18
  • 37
0

Your php code seems right, however you try to access the file using the "picture" key of the $_FILES global. It does not seems to be specified in your csharp code. I don't know how to do it thought. You could try to see how it was named in your php by doing a print_r or vardump of you $_FILE global or using the array_keys function

Regards

Edit: I found this link that could help you to add a "name" to your uploaded file: http://www.bratched.com/en/home/dotnet/69-uploading-multiple-files-with-c.html

grifos
  • 3,321
  • 1
  • 16
  • 14
  • i have just assigned it as picture. actually what does $_FILES["pictures"]["error"] code mean? – Fawzan Izy Apr 19 '12 at 23:37
  • This answer is wrong. The PHP code in the question is for handling multiple file uploads, and only works when `` is used. – Xenon Apr 19 '12 at 23:56
  • Yes this is wrong. It was not meant to upload mutliple files as @Xenon said. – Fawzan Izy Apr 20 '12 at 16:08
  • I not linked this to show you how to upload, just the quickest thing i could find to show you how to set a name for the uploaded file.`Content-Disposition: form-data; name=\"{0}\"{1}{1}"` was the only thing i wanted to show you. To explain $_FILE is a global array containing the uploaded file. It is a global array like the $_POST or $_GET ones. When you try to access a value in those arrays you use a key, which is called a 'name'. Like xenon example of html file input, the attribute name is set and that allow you to access the $_FILE array with the same name. Thing that i don't see in your c#. – grifos Apr 20 '12 at 22:48