Random Crash Image

camarocarshwhite.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 (30)
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
19. 09-01-2011 03:07
thanks... here is the async version, too
Thanks, mate. I really needed this example. Let me share my version of the client (actually it's c++, but the .net objects are ther same). This one uploads a file in async mode, so one can show a progressbar or something. 
 
static void UploadFileCallback(Object^ sender, UploadFileCompletedEventArgs^ e) 

array^ response = e->Result; 
Globals::uploadResult = System::Text::Encoding::UTF8->GetString(response); 
Globals::isUploading = false; 
Globals::isUploadComplete = true; 

static void UploadProgressCallback(Object^ sender, UploadProgressChangedEventArgs^ e) 

Globals::uploadPercent = (int)(100*e->BytesSent/e->TotalBytesToSend); 

 
void UploadFileHTTPAsync(String^ fileName) 

Net::WebClient^ client = gcnew Net::WebClient(); 
Uri^ uri = gcnew Uri("http://myserver.com/upload.php"); 
client->Headers->Add("Content-Type","binary/octet-stream"); 
 
client->UploadFileCompleted +=  
gcnew UploadFileCompletedEventHandler (UploadFileCallback); 
 
// Specify a progress notification handler. 
client->UploadProgressChanged +=  
gcnew UploadProgressChangedEventHandler( UploadProgressCallback ); 
client->UploadFileAsync( uri , "POST", fileName ); 
Globals::isUploading = true; 
Globals::isUploadComplete = false; 
}
Guest
joco
20. 12-08-2010 20:04
Thanks
Thanks for posting this! I know nothing about PHP, so this helped a lot!
Guest
Lander
21. 02-10-2011 23:26
Gracias
Muchas gracias me sirvio bastante :grin
Guest
Cristian
22. 02-10-2011 23:24
Thank You So Much
We deadly need this.....so thank you so much....its working...... :) :)
Guest
Manish Soni
23. 02-10-2011 23:27
Thank You So Much
En donde se guarda los archivos? 
/upload/ no hay nada! 
 
path save the files? 
/upload/ not found 
path upload exist
Guest
lucas
24. 02-10-2011 23:22
thank alot
how about if i want to call an upload function? lets say i have page.php and there\'s alot of functions and i only want to call the upload function? can you help me? thanks
Guest
Gin
25. 02-10-2011 23:19
thank alot
At least it's working! :) Thank you very much!
Guest
Phil
26. 22-10-2011 02:49
Thanks man!
It works great. That what I need!
Guest
Yves
27. 25-10-2011 06:06
Thanks!!
But I got upload failed. I dont understand this part:  
 
$uploaddir = 'upload/'; indicates what here?  
 
Really really appreciate if you can help me out here. thank you.
Guest
Terry
28. 01-02-2012 22:01
re-post
So sorry for my mistake, it works like a charm! very appreciate for your great work! That's exactly what I need.
Guest
terry
29. 02-12-2011 10:08
cheers to you
Again, 
exactly what I was looking for. 
Right now I have an application which uses ftp to upload files, but this is less secure since I want to trigger a validating php script upon upload. 
thank you
Guest
Ilan
30. 01-02-2012 22:01
problem
hi, it\'s good when there is no space in the filename, but for the file \"C:\\my file.mp3\" 
its not working at all :( 
any suggestion ? 
thx anyway
Guest
jeremy

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: 959648

Who's Online

We have 1 guest online

Paypal Donations

© 2012 DngloZ