Upload a file to a webserver using C# and PHP

T

Tim.Geiges

I was looking for a simple way to upload a file to a webserver from a
standalone C# windows application, I had a lot of trouble finding a
simple solution but ran a simple way(note: I have not checked to see
what the filesize limitations and stuff are but works great so far,
most likely the size limit is specified in the php.ini, but can be
overridden in your script)

Anyway this just simply passes the file to a php script on a webserver
and moves it to the folder specified in your php script, or you could
pass a directory as a variable I guess :)

===========================================================
C# Upload file to PHP Script

===========================================================
C# Part ------
using System.Net;
....

//THIS UPLOADS THE FILE THROUGH A PHP SCRIPT ON THE SERVER.
WebClient client = new WebClient();
client.UploadFile("https://www.mywebsite.com/uploadtome.php","c:\\File.txt");


===========================================================
PHP Part ------

<?php
$ftmp = $_FILES['file']['tmp_name'];
$oname = $_FILES['file']['name'];
$fname = '/Textfiles/' . $oname;
if(move_uploaded_file($ftmp, $fname)){
echo "Done";
exit();
} else {
echo "Failed";
}
?>

===========================================================

Tim Geiges
(e-mail address removed)
PHP Programmer
LoanToolbox.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top