{"id":309,"date":"2013-09-21T22:49:18","date_gmt":"2013-09-21T14:49:18","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=309"},"modified":"2018-09-04T13:06:32","modified_gmt":"2018-09-04T05:06:32","slug":"how-to-receive-http-post-data-and-a-file-from-a-c-program-using-php","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-receive-http-post-data-and-a-file-from-a-c-program-using-php\/","title":{"rendered":"How to receive HTTP post data and a file from a C# program using PHP"},"content":{"rendered":"<p>Some time ago, I wrote a post on <a href=\"http:\/\/www.techcoil.com\/blog\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" title=\"Sending a file and some form data via HTTP post in C#\" target=\"_blank\">how to send some data and a file from C#<\/a>, via HTTP post, to a PHP script. I called that script <code>GetPostRequest.php<\/code>.<\/p>\n<p>This post explains the internals of the script in handling the data received from the C# program. <\/p>\n<p>Note that we can also present a form to our favourite browser to submit the same kind of data to <code>GetPostRequest.php<\/code>.<\/p>\n<h2>Accessing the data received from the C# \/ HTTP client in PHP<\/h2>\n<p>Recall that we had sent the following data to GetPostRequest.php with the following names (keys):<\/p>\n<ul>\n<li>myFileDescription<\/li>\n<li>myFile<\/li>\n<\/ul>\n<p>Some text that describes the file will be associated with the <code>myFileDescription<\/code> key, while data related with the uploaded file will be associated with the <code>myFile<\/code> key. <\/p>\n<p>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:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nif (isset($_POST&#x5B;'myFileDescription']) &amp;&amp; isset($_FILES&#x5B;'myFile']) ) {\r\n    handlePostDataFromClient();\r\n}\r\n?&gt;\r\n<\/pre>\n<p>The text data should be available in the <code>$_POST<\/code> variable, via the key <code>myFileDescription<\/code>, while the data related to the uploaded file in the <code>$_FILES<\/code> variable. I test for their existence with the <code>isset<\/code> function. <\/p>\n<p>When the text data and file upload data are available, my script will proceed to call the code>handlePostDataFromClient<\/code> function to handle them.<\/p>\n<h2>Handling the text data and file upload data from the client <\/h2>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nfunction handlePostDataFromClient() {\r\n\r\n    $fileDescription = $_POST&#x5B;'myFileDescription'];\r\n    $filePath = handleFileUploadData();\r\n    if ($filePath != NULL) {\r\n        \/\/ saveDetailsToDatabase($fileDescription, $fileDescription);\r\n    }\r\n    else {\r\n\theader('HTTP\/1.0 500 Internal Server Error');\r\n    }\r\n}\r\n?&gt;\r\n<\/pre>\n<p>Inside the <code>handlePostDataFromClient<\/code> function, I save the text data in a variable and call the <code>handleFileUploadData<\/code> function to handle the file upload.<\/p>\n<p>When I am able to receive a file path from the <code>handleFileUploadData<\/code> 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.<\/p>\n<h3>Details of the $_FILES['myFile']<\/h3>\n<p>Prior to coding <code>handleFileUploadData<\/code>, I first issue a call to <code>print_r<\/code> on <code>$_FILES['myFile']<\/code>. With that, I got the following when the HTTP post was performed successfully by the client:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt;_MG_0867.JPG\r\n    &#x5B;type] =&gt;image\/jpeg\r\n    &#x5B;tmp_name] =&gt;G:\\Windows\\Temp\\php2E9F.tmp\r\n    &#x5B;error] =&gt; 0\r\n    &#x5B;size] =&gt; 6444620\r\n)\r\n<\/pre>\n<p>After I got a glimpse of the <code>$_FILES['myFile']<\/code> array, I proceeded to code the <code>handleFileUploadData<\/code> function:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nfunction handleFileUploadData() {\r\n\t\r\n    $success = ($_FILES&#x5B;'myFile']&#x5B;'error'] == 0); \r\n    if ($success) {\r\n        $filePath = 'G:\\\\uploadedFiles\\\\' . time() . $_FILES&#x5B;'myFile']&#x5B;'name'];\r\n \t$success = copy($_FILES&#x5B;'myFile']&#x5B;'tmp_name'], $filePath);\r\n    }\r\n\r\n    if ($success) {\r\n        return $filePath;\r\n    }\r\n    else {\r\n\treturn NULL;\r\n    }\r\n}\r\n?&gt;\r\n<\/pre>\n<p>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, <code>$_FILES['myFile']['error']<\/code> will contain 0. <\/p>\n<p>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 <code>$_FILES['myFile']['name']<\/code> variable.<\/p>\n<p>I then proceed to perform the file copying by issuing a call to the <code>copy<\/code> function, passing it <code>$_FILES['myFile']['tmp_name']<\/code> and the <code>$filePath<\/code> variable. The return value of the copy function will allow me to know if the copying is successful.<\/p>\n<p>If the copying of the file is successful, I return the destination file path; if not, I return a NULL back to the caller.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-4Z\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4Z&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4Z&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Some time ago, I wrote a post on <a href=\"http:\/\/www.techcoil.com\/blog\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" title=\"Sending a file and some form data via HTTP post in C#\" target=\"_blank\">how to send some data and a file from C#<\/a>, via HTTP post, to a PHP script. I called that script <code>GetPostRequest.php<\/code>.<\/p>\n<p>This post contains the internals of the script in handling the data received from the C# program. <\/p>\n","protected":false},"author":1,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[166,23,34,161,13],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PHP-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-4Z","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/309"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}