{"id":237,"date":"2012-02-08T19:50:31","date_gmt":"2012-02-08T19:50:31","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=237"},"modified":"2022-02-17T07:14:05","modified_gmt":"2022-02-17T07:14:05","slug":"upload-a-file-from-a-form","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/platforms\/aws\/s3\/upload-a-file-from-a-form","title":{"rendered":"Upload A File From A Form"},"content":{"rendered":"<h4>\nBasic Upload With Page Hanging Until Upload Completes<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n&lt;?php\r\ninclude_once(&#39;config.php&#39;);\r\nrequire &#39;awssdk\/aws.phar&#39;;\t\t\t\/\/&lt;SDK so we can access S3 storage\r\n\r\n\t\tglobal $AwsS3BucketName;\r\n\t\tglobal $AwsS3AccessKeyId;\r\n\t\tglobal $AwsS3AccessSecretKey;\r\n\t\t\r\n\r\n\t\t$sdk = new Aws\\Sdk([\r\n\t\t\t\t&#39;version&#39; =&gt; &#39;latest&#39;,\r\n\t\t\t\t&#39;region&#39;  =&gt; &#39;eu-west-1&#39;,\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt; REGION\r\n\t\t\t\t&#39;credentials&#39; =&gt; [\r\n\t\t\t\t\t\t&#39;key&#39;    =&gt; $AwsS3AccessKeyId,\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 ACCESS KEY ID\r\n\t\t\t\t\t\t&#39;secret&#39; =&gt; $Aw\r\n\r\nif (isset($_POST['submit']))\r\n{\r\n\t\/\/----- FORM SUBMITTED - UPLOAD FILE -----\r\n\r\n\tif (filesize($_FILES[&#39;file_uploaded1&#39;]['tmp_name']) &gt; 0)\r\n\t{\r\n\t\t\/\/Retrieve post variables\r\n\t\t$file = $_FILES[&#39;file_uploaded1&#39;];\r\n\t\t$filename = $file['name'];\t\t\t\t\/\/This is the source filename\r\n\t\t$tmpname = $file['tmp_name'];\t\t\t\/\/This is where the file is temporarily stored\r\n\r\n\t\t$keyName = $filename;\t\t\t\/\/&lt;&lt;&lt;This will be the filename\r\n\r\n\t\t\/\/Upload the file\r\n\t\ttry\r\n\t\t{\r\n\t\t\t$s3Client-&gt;putObject(\r\n\t\t\t\tarray(\r\n\t\t\t\t\t&#39;Bucket&#39;=&gt;$AwsS3BucketName,\r\n\t\t\t\t\t&#39;Key&#39; =&gt;  $keyName,\r\n\t\t\t\t\t&#39;SourceFile&#39; =&gt; $tmpname,\r\n\t\t\t\t\t&#39;StorageClass&#39; =&gt; &#39;REDUCED_REDUNDANCY&#39;,\t\t\/\/&lt;&lt;&lt;Optional parameter\r\n\t\t\t\t\t&#39;ACL&#39; =&gt; &#39;public-read&#39;\t\t\t\t\t\t\t\t\t\t\/\/&lt;&lt;&lt;Optional parameter\r\n\t\t\t\t)\r\n\t\t\t);\r\n\t\t}\r\n\t\tcatch (S3Exception $e)\r\n\t\t{\r\n\t\t\techo $e-&gt;getMessage();\r\n\t\t}\r\n\t\tcatch (Exception $e)\r\n\t\t{\r\n\t\t\techo $e-&gt;getMessage();\r\n\t\t}\r\n\t}\r\n}\r\n?&gt;\r\n\r\n&lt;h1&gt;Upload a file&lt;\/h1&gt;\r\n&lt;form action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot; name=&quot;form&quot; id=&quot;form&quot;&gt;\r\n        &lt;label&gt;Send this file&lt;\/label&gt;\r\n        &lt;input name=&quot;file_uploaded1&quot; type=&quot;file&quot; \/&gt;\r\n        &lt;input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;Upload&quot;&gt;\r\n&lt;\/form&gt;\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Basic Upload With Page Hanging Until Upload Completes &lt;?php include_once(&#39;config.php&#39;); require &#39;awssdk\/aws.phar&#39;; \/\/&lt;SDK so we can access S3 storage global $AwsS3BucketName; global $AwsS3AccessKeyId; global $AwsS3AccessSecretKey; $sdk = new Aws\\Sdk([ &#39;version&#39; =&gt; &#39;latest&#39;, &#39;region&#39; =&gt; &#39;eu-west-1&#39;, \/\/&lt;&lt;&lt;&lt;&lt; REGION &#39;credentials&#39; =&gt; [ &#39;key&#39; =&gt; $AwsS3AccessKeyId, \/\/&lt;&lt;&lt;&lt;&lt; S3 ACCESS KEY ID &#39;secret&#39; =&gt; $Aw if (isset($_POST[&#8216;submit&#8217;])) { \/\/&#8212;&#8211; [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-237","post","type-post","status-publish","format-standard","hentry","category-s3"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/237","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=237"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":1891,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/237\/revisions\/1891"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}