Geeks With Blogs
Pranav`s TechNiche technical thoughts of a coffee addicted developer
          I was fed up with uploading the attachments using FTP as it was too slow (and vulnerable too), maintaining credentials to connect to server, managing permissions to specific folder etc. and all that nasty stuff.  Clients have also complained on this. After head-scratching and wall banging efforts, I planned to search for an alternative for FTP to upload the files. Found this way... Its pretty cool and uploading files much faster compared to FTP approach. Just wanted to share it with you, hoping that it may help someone who are in need!

This is a small tutorial which will teach you how to upload files using a C# client application to a server running PHP.

We’ll call the PHP Script “upload.php”, this is what it should contain:

<?php
    $uploaddir = ‘upload/’; // Relative Upload Location of data file
    if (is_uploaded_file($_FILES[‘file’][‘tmp_name’]))
    {
        $uploadfile = $uploaddir . basename($_FILES[‘file’][‘name’]);
        echo “File “. $_FILES[‘file’][‘name’] .” uploaded successfully. “;
        if (move_uploaded_file($_FILES[‘file’][‘tmp_name’], $uploadfile))
        {
            echo “File is valid, and was successfully moved. “;
        }

        else
            print_r($_FILES);
    }
    else
    {
        echo “Upload Failed!!!”;
        print_r($_FILES);
    }
?>


and here is the C# code...

    System.Net.WebClient Client = new System.Net.WebClient ();
    Client.Headers.Add("Content-Type","binary/octet-stream");
    byte[] result = Client.UploadFile ("http://your_server/upload.php","POST","C:\test.jpg");
    String s = System.Text.Encoding .UTF8 .GetString (result,0,result.Length );

         In the C# part, replace “your_server.com” with your server, also notice that this is a test code, it will upload c:\\test.jpg, because I am testing with an image file, I am using the header “binary/octet-stream” as it works with all files (image, txt, etc)…

Hope this helps someone out there as I couldn’t find any tutorials about this specific matter on googling!
Posted on Monday, October 26, 2009 1:10 PM .NET | Back to top


Comments on this post: Upload file using C# as Client and PHP as Server

# re: Upload file using C# as Client and PHP as Server
Requesting Gravatar...
Thank you, sir!
This is very helpful!
Left by Burgos on Nov 21, 2010 6:43 PM

# re: Upload file using C# as Client and PHP as Server
Requesting Gravatar...
Thanks for help. There is a small error in the code. In c# code

replace C:\test.jpg to C:\\test.jpg

Very helpful and fast code.
Left by Agrima on Jul 15, 2011 8:44 PM

# re: Upload file using C# as Client and PHP as Server
Requesting Gravatar...
Thanks dude ........its very helpful
Left by jitendra on Aug 31, 2011 2:13 PM

# re: Upload file using C# as Client and PHP as Server
Requesting Gravatar...

Thanks very much.. u saved my lot of time..
Left by salterj on Feb 14, 2012 8:29 AM

Your comment:
 (will show your gravatar)


Copyright © Pranav | Powered by: GeeksWithBlogs.net