Random Crash Image

360crsh4.jpg
Upload a file using c# as client to a PHP server
Written by Administrator   
Friday, 11 May 2007

w00t, our first c# and PHP tutorial =0) I'm new to c# and .net alltogether and therefor I thought it would be a good idea to write about what i learn along with time, please feel free to comment if you find something wrong, or have any questions regarding the code...

Today I learned on how to upload a file using C# to a PHP server.

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 this 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'm testing with an image file, im 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 google! Let me know if you got any questions.


Quote this article on your site

Comments (18)
RSS comments
1. 15-05-2007 15:33
Thanks a lot
This is just the example I was looking for. Thanks.
Guest
Fran
2. 15-05-2007 16:19
=0)
Glad it helped someone out! I recently added a way to check for username/password, i'll post that soon.
Guest
DngloZ
3. 21-05-2007 15:32
thanks!
This is what I needed!!  
But I'm trying to use it in a windows mobile 5 application and System.Net.Webclient isn't imported in the .NET compact framework. Do you know a work around for this?
Guest
Pieter
4. 12-06-2007 13:46
asda
:roll
Guest
Anand
5. 10-03-2008 20:17
asda
thanks! just what i needed to see!
Guest
zlarsonDOTcom
6. 01-04-2008 16:06
it helped
it helped,thanks :)
Guest
raj
7. 24-04-2008 13:20
it helped but...
thanks a lot..it helped me 
but i cannot send over 10 mb xml data :cry  
what i am to do?
Guest
zafer
8. 25-04-2008 08:56
it helped but...
I think that's the default max file upload size in php, u have to check the settings (i think php.ini file) and increase it.
Guest
Admin
9. 23-05-2008 11:02
it helped but...
:) ammmmmaazing! thanks exactly what i needed
Guest
Kajal
10. 19-06-2008 14:17
Compact Framework
Has anyone found a way to upload from .Net Compact Framework to a PHP server?
Guest
Chris
11. 26-01-2009 17:38
Compact Framework
Small bug, it should be (with @) 
 
... @"C:\test.jpg");
Guest
Jiga
12. 18-02-2009 10:12
Helped alot.
Hey, I found this tutorial really useful, it's something I havent seen elsewhere much. 
 
Thanks :)
Guest
Tony
13. 17-02-2009 18:00
WebClient
Excellent!!!!
Guest
Mike
14. 10-03-2009 00:30
Gracias
Gracias, funciona perfecto. 
 
Thanks
Guest
Daniel
15. 15-05-2009 14:18
Gracias
Thank You. Thank You. 
Thank You. 
You helped me lot.
Guest
Peter
16. 18-11-2009 18:59
Like every one say
Thank you. This is one piece of code which is hard to find and you have helped many people a lot of trouble / frustration (including me!) 
 
Excellent and thank you
Guest
VG
17. 27-01-2010 03:59
Thank you
i wished all tutorials were as easy at this. 
 
they write 5 pages for a tutorial with 30 lines :) 
 
this is just how it should be. 
thank you very much
Guest
Ahmet Yildiz
18. 03-02-2010 09:59
how about downloading a file from a serv
how about downloading a file from a PHP server?while running that file in a c# application?
Guest
aybie

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
Title:
Comment:



Code:* Code

Powered by AkoComment Tweaked v.1.4.2

Last Updated ( Wednesday, 04 July 2007 )
 
< Prev

Statistics

Visitors: 683527

Who's Online

We have 1 guest online

Paypal Donations

© 2010 DngloZ