Upload using C++ as Client and PHP as Server

Level: Beginner (coder)

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

I had previously posted a version for C# that can be found here: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-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:
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;
}

In the C++ part, replace “your_server.com” with your server, also notice that this is a test code, 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,

Happy Coding :)

This entry was posted in Apache, C#, Client-Server, PHP, Programming, Tutorials. Bookmark the permalink.

4 Responses to Upload using C++ as Client and PHP as Server

  1. sajid says:

    am using dev-c++ but this code not working. can you some explain me with easy

  2. Ad Fundum says:

    Hi there,

    I get this error on the first line of the c++ script: variable or field `UploadFileCallback’ declared void

    Yes, I am using c++ and not c. What else could be wrong?

    Thanks for your time!

    • Ad Fundum says:

      I think I am missing an include or a header or something, as many things of the code don’t get recognised…

  3. Matt says:

    Its nice to see the publisher is replying to people asking for help….

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>