How to receive HTTP post data and a file from a C# program using PHP
Some time ago, I wrote a post on how to send some data and a file from C#, via HTTP post, to a PHP script. I called that script GetPostRequest.php
.
This post explains the internals of the script in handling the data received from the C# program.
Note that we can also present a form to our favourite browser to submit the same kind of data to GetPostRequest.php
.
Accessing the data received from the C# / HTTP client in PHP
Recall that we had sent the following data to GetPostRequest.php with the following names (keys):
- myFileDescription
- myFile
Some text that describes the file will be associated with the myFileDescription
key, while data related with the uploaded file will be associated with the myFile
key.
Before I proceed to handle the data, I first create a conditional to test whether the two pieces of data are successfully detected by my PHP engine:
<?php if (isset($_POST['myFileDescription']) && isset($_FILES['myFile']) ) { handlePostDataFromClient(); } ?>
The text data should be available in the $_POST
variable, via the key myFileDescription
, while the data related to the uploaded file in the $_FILES
variable. I test for their existence with the isset
function.
When the text data and file upload data are available, my script will proceed to call the code>handlePostDataFromClient function to handle them.
Handling the text data and file upload data from the client
<?php function handlePostDataFromClient() { $fileDescription = $_POST['myFileDescription']; $filePath = handleFileUploadData(); if ($filePath != NULL) { // saveDetailsToDatabase($fileDescription, $fileDescription); } else { header('HTTP/1.0 500 Internal Server Error'); } } ?>
Inside the handlePostDataFromClient
function, I save the text data in a variable and call the handleFileUploadData
function to handle the file upload.
When I am able to receive a file path from the handleFileUploadData
function, I proceed to save the file description and file path to the database. If I receive a NULL from the function, I will return a HTTP 500 internal server error back to the C# client.
Details of the $_FILES['myFile']
Prior to coding handleFileUploadData
, I first issue a call to print_r
on $_FILES['myFile']
. With that, I got the following when the HTTP post was performed successfully by the client:
Array ( [name] =>_MG_0867.JPG [type] =>image/jpeg [tmp_name] =>G:\Windows\Temp\php2E9F.tmp [error] => 0 [size] => 6444620 )
After I got a glimpse of the $_FILES['myFile']
array, I proceeded to code the handleFileUploadData
function:
<?php function handleFileUploadData() { $success = ($_FILES['myFile']['error'] == 0); if ($success) { $filePath = 'G:\\uploadedFiles\\' . time() . $_FILES['myFile']['name']; $success = copy($_FILES['myFile']['tmp_name'], $filePath); } if ($success) { return $filePath; } else { return NULL; } } ?>
I first check whether the PHP engine can successfully create the file from the HTTP request received from my C# client and make it available at some temporary folder on my folder. If it is successful in doing so, $_FILES['myFile']['error']
will contain 0.
I then derive a destination file path for copying the file from the temporary folder to a permanent location. I derive the destination file path with the uploaded file name, the current timestamp value and the folder where I want to save the file into. The uploaded file name is available in the $_FILES['myFile']['name']
variable.
I then proceed to perform the file copying by issuing a call to the copy
function, passing it $_FILES['myFile']['tmp_name']
and the $filePath
variable. The return value of the copy function will allow me to know if the copying is successful.
If the copying of the file is successful, I return the destination file path; if not, I return a NULL back to the caller.