{"id":248,"date":"2019-01-18T09:01:37","date_gmt":"2019-01-18T14:01:37","guid":{"rendered":"http:\/\/www.joonim.org\/?p=248"},"modified":"2019-01-18T09:01:37","modified_gmt":"2019-01-18T14:01:37","slug":"php-uploading-a-file","status":"publish","type":"post","link":"https:\/\/waterloobae.com\/blog\/2019\/01\/18\/php-uploading-a-file\/","title":{"rendered":"[PHP] How to Upload a file"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Not like any other post variables, file element information is posted as two dimensional array. So, getting post data for a file element can be accomplished as below.<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">&lt;?php\n  if(isset($_POST['btnUpload'])){\n    $name = $_FILES['upfile']['name'];\n    $tmpname = $_FILES['upfile']['tmp_name'];\n    $sizer = $_FILES['upfile']['size'];\n    \n    \/\/ open file to read data;    \n    $fp = fopen($tmpname, 'r'); \n    \/\/ You can do whatever you need to from this point.\n    \/\/ The example below reads the contents of uploaded text file.\n    $content = fread($fp, filesize($tmpname));\n    fclose($fp);\n    \/\/ break data from csv into array line by line\n    $data = preg_split('\/\\r\\n|\\r|\\n\/', $content); \n?&gt;\n\n&lt;FORM action=\"&lt;?=$_SERVER['PHP_SELF']=&gt;\"  method=\"post\" enctype=\"multipart\/form-data\"&gt;\n&lt;input name=\"upfile\" id=\"upfile\" type=\"file\"&gt;\n&lt;input value=\"Upload\" name=\"btnUpload\" id=\"btnUpload\" type=\"submit\"&gt;\n&lt;\/FORM&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Not like any other post variables, file element information is posted as two dimensional array. So, getting post data for a file element can be accomplished as below.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-it-tips"],"_links":{"self":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":0,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"wp:attachment":[{"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/waterloobae.com\/blog\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}