Click here to Skip to main content
13,592,724 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 31 Jan 2012
Licenced CPOL

Upload a file using C# as client to a PHP server

, 7 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Upload a file using c# as client to a PHP server

In my recent project I had to upload a file to a PHP server from a client application developed in C#. I found this method and I thought to share this with you.

First you have to use the namespace. Then you have to create a webclient object. Using the webclient object you can upload your file to a PHP file in the server.

using System.Net;
WebClient cl = new WebClient();

    cl.UploadFile("http://" + ip + "/test.php", file);
catch(Exception e)
    MessageBox.Show("Upload failed");

Now you can access the file from the PHP file. In the below example I create a folder in the server machine and move the file into the folder.

//check whether the folder the exists
  //create the folder
  //give permission to the folder
  chmod('C:/Users/dhanu-sdu/Desktop/test', 0777);

//check whether the file exists
if (file_exists('C:/Users/dhanu-sdu/Desktop/test/'. $_FILES["file"]["name"]))
  echo $_FILES["file"]["name"] . " already exists. ";
  //move the file into the new folder
  move_uploaded_file($_FILES["file"]["tmp_name"],'C:/Users/dhanu-sdu/Desktop/test/'. $_FILES["file"]["name"]);



Also, you can download data from a PHP server and display it in a C# web browser by using the following code.

WebClient cl = new WebClient();
    byte[] response = cl.DownloadData("http://" + ip +"/test.php");
    webBrowser1.DocumentText = System.Text.ASCIIEncoding.ASCII.GetString(response);
catch(Exception e)
    MessageBox.Show("Download failed");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dhanushka Madushan lk
Software Developer
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNot an article Pin
Manas Bhardwaj6-Jun-12 22:36
memberManas Bhardwaj6-Jun-12 22:36 
GeneralInteresting way to connect a desktop-based applications to a... Pin
innuendoreplay31-Jan-12 14:58
memberinnuendoreplay31-Jan-12 14:58 
GeneralI used this code to upload a file to a php server and it wor... Pin
Dhanushka Madushan Peiris31-Jan-12 14:01
memberDhanushka Madushan Peiris31-Jan-12 14:01 
GeneralReason for my vote of 1 WebClient should be placed in a usin... Pin
pietvredeveld31-Jan-12 8:02
memberpietvredeveld31-Jan-12 8:02 
GeneralReason for my vote of 1 title is unrelated, nothing new, upl... Pin
Selvin31-Jan-12 6:52
memberSelvin31-Jan-12 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180618.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by Dhanushka Madushan lk
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid