{"id":4620,"date":"2024-05-09T10:18:58","date_gmt":"2024-05-09T09:18:58","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4620"},"modified":"2025-12-02T22:03:24","modified_gmt":"2025-12-02T22:03:24","slug":"post-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/files\/post-files","title":{"rendered":"POST Files"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Handling a file passed as a POST request<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/-----------------------------------\n    \/\/----- CHECK FOR FILE UPLOADED -----\n    \/\/-----------------------------------\n    $FileUploadValid = True;      \/\/Default to valid  file being uploaded\n    \n    if (!isset($_FILES&#91;'UploadedFile']))      \/\/&lt;&lt;&lt;&lt; 'UploadedFile' is the name of our file field in the post data\n      $FileUploadValid = False;\n    \n    \/\/Check the files array isn't empty\n    if(!empty( $_FILES ))\n    {\n      \/\/Verify that the file being uploaded is one of our supported types\n      \/\/if (!is_valid_image_file_type($_FILES&#91;'UploadedFile']&#91;'type']))\n      \/\/  $FileUploadValid = False;\n    }\n    else\n    {\n      $FileUploadValid = False;\n    }\n    \n    \n    if ($FileUploadValid)\n    {\n      \/\/--------------------------\n      \/\/----- STORE THE FILE -----\n      \/\/--------------------------\n            \n      \/\/Get the file extension\n      $UploadedFileName = $_FILES&#91;'UploadedFile']&#91;'name'];    \/\/&lt;&lt;&lt;&lt; 'UploadedFile' is the name of our file field in the post data\n      $UploadedFileNamePreExtension = strtolower(pathinfo($UploadedFileName, PATHINFO_FILENAME));\n      $UploadedFileExtension = strtolower(pathinfo($UploadedFileName, PATHINFO_EXTENSION));\n      if (strlen($UploadedFileExtension) > 4)\n        $UploadedFileExtension = 'err';\n      \n      $FileId = 0;\n      if (is_file($_FILES&#91;'UploadedFile']&#91;'tmp_name']))   \/\/&lt;&lt;&lt;&lt; 'UploadedFile' is the name of our file field in the post data. 'tmp_name' is the name the server has temporarily given the file\n      {\n        \/\/----- ADD TO OUR DB -----\n        $FileContent = file_get_contents($_FILES&#91;'UploadedFile']&#91;'tmp_name']);\n        $FileId = db_files_add_new($DeviceId, $UploadedFileName, 1, $FileContent);\n\n        \n        \/\/----- WRITE TO FILE -----\n        $SaveAsFilename = \"uploadedfile.txt\";             \/\/&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;Set this\n        $OurFileUploadsDirectory = $_SERVER&#91;'DOCUMENT_ROOT'] . '\/uploaded_files';\n\n        \/\/Ensure directory exists\n        if (!file_exists($OurFileUploadsDirectory))\n        {\n          wp_mkdir_p($OurFileUploadsDirectory);\n          chmod($OurFileUploadsDirectory, 02775);     \/\/Set folder permissions to allow read of file added here\n        }\n\n        \/\/Write the file\n        $Source = $_FILES&#91;'UploadedFile']&#91;'tmp_name'];      \/\/&lt;&lt;Change 'UploadedFile' to whatever name is used for the File field\n        $OurFileUploadsDirectory = trailingslashit($OurFileUploadsDirectory);\n        move_uploaded_file($Source, $OurFileUploadsDirectory . $SaveAsFilename);\n\n        if (!is_file($OurFileUploadsDirectory . $SaveAsFilename))\n        {\n          \/\/----- FILE SAVE FAILED -----\n\n        }        \n        \n      } \/\/if (is_file($_FILES&#91;'UploadedFile']&#91;'tmp_name']))\n\n    } \/\/if ($FileUploadValid)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Handling a file passed as a POST request<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-4620","post","type-post","status-publish","format-standard","hentry","category-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4620"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4620\/revisions"}],"predecessor-version":[{"id":5209,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4620\/revisions\/5209"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}