{"id":576,"date":"2019-07-19T04:17:03","date_gmt":"2019-07-19T04:17:03","guid":{"rendered":"http:\/\/info.juliusgoh.life\/?p=576"},"modified":"2019-09-10T06:16:14","modified_gmt":"2019-09-10T06:16:14","slug":"php-curl-with-attachment-curlfile","status":"publish","type":"post","link":"https:\/\/info.juliusgoh.life\/?p=576","title":{"rendered":"PHP Curl with attachment ( CurlFile)"},"content":{"rendered":"<h1>Sender PHP Code<\/h1>\n<pre>&lt;form method=\"post\" action=\"&lt;?=$_SERVER[\"PHP_SELF\"] ?&gt;\" enctype=\"multipart\/form-data\"&gt;\r\n    &lt;input name=\"file\" type=\"file\" \/&gt;\r\n    &lt;input type=\"submit\" value=\"Upload\" \/&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;?php\r\nif(!empty($_FILES)){\r\n    $target=\"https:\/\/juliusgoh.life\/tut\/attachment-receiver.php\";\r\n\r\n    echo \"&lt;pre&gt;\";\r\n    print_r($_FILES);   \r\n    echo \"&lt;\/pre&gt;\";\r\n    # http:\/\/php.net\/manual\/en\/curlfile.construct.php\r\n\r\n    \/\/ Create a CURLFile object \/ procedural method \r\n    $cfile = curl_file_create($_FILES[\"file\"][\"tmp_name\"],$_FILES[\"file\"][\"type\"],$_FILES[\"file\"][\"name\"]); \/\/ try adding \r\n\r\n    \/\/ Create a CURLFile object \/ oop method \r\n    #$cfile = new CURLFile(\"resource\/test.png\",\"image\/png\",\"testpic\"); \/\/ uncomment and use if the upper procedural method is not working.\r\n\r\n    \/\/ Assign POST data\r\n    $imgdata = array(\"file\" =&gt; $cfile);\r\n\r\n    $curl = curl_init();\r\n    curl_setopt($curl, CURLOPT_URL, $target);\r\n    #curl_setopt($curl, CURLOPT_USERAGENT,\"Opera\/9.80 (Windows NT 6.2; Win64; x64) Presto\/2.12.388 Version\/12.15\");\r\n    curl_setopt($curl, CURLOPT_HTTPHEADER,array(\"Content-Type: multipart\/form-data\"));\r\n    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); \/\/ stop verifying certificate\r\n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); \r\n    curl_setopt($curl, CURLOPT_POST, true); \/\/ enable posting\r\n    curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); \/\/ post images \r\n    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); \/\/ if any redirection after upload\r\n    $r = curl_exec($curl); \r\n    curl_close($curl);\r\n\r\n    echo \"&lt;h1&gt;Result&lt;\/h1&gt;\";\r\n    echo \"&lt;pre&gt;\";\r\n    print_r($r);\r\n    echo \"&lt;\/pre&gt;\";\r\n}\r\n?&gt;\r\n<\/pre>\n<h1>Receiver PHP Code<\/h1>\n<pre>&lt;?php\r\n\r\n$uploaddir = realpath(\".\/\") . \"\/\";\r\n$uploadfile = $uploaddir . basename($_FILES[\"file\"][\"name\"]);\r\nif (move_uploaded_file($_FILES[\"file\"][\"tmp_name\"], $uploadfile)) {\r\n    echo \"File is valid, and was successfully uploaded.\\n\";\r\n} else {\r\n    echo \"Possible file upload attack!\\n\";\r\n}\r\n\r\necho \"&lt;pre&gt;\";\r\necho \"Here is some more debugging info:\";\r\nprint_r($_FILES);\r\necho \"\\n&lt;hr \/&gt;\\n\";\r\nprint_r($_POST);\r\necho \"&lt;\/pre&gt;\";\r\n?&gt;\r\n<\/pre>\n<hr \/>\n<p>Ref : <a href=\"https:\/\/www.php.net\/manual\/en\/class.curlfile.php\">https:\/\/www.php.net\/manual\/en\/class.curlfile.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sender PHP Code &lt;form method=&#8221;post&#8221; action=&#8221;&lt;?=$_SERVER[&#8220;PHP_SELF&#8221;] ?&gt;&#8221; enctype=&#8221;multipart\/form-data&#8221;&gt; &lt;input name=&#8221;file&#8221; type=&#8221;file&#8221; \/&gt; &lt;input type=&#8221;submit&#8221; value=&#8221;Upload&#8221; \/&gt; &lt;\/form&gt; &lt;?php if(!empty($_FILES)){ $target=&#8221;https:\/\/juliusgoh.life\/tut\/attachment-receiver.php&#8221;; echo &#8220;&lt;pre&gt;&#8221;; print_r($_FILES); echo &#8220;&lt;\/pre&gt;&#8221;; # http:\/\/php.net\/manual\/en\/curlfile.construct.php \/\/ Create a CURLFile object \/ procedural method $cfile = curl_file_create($_FILES[&#8220;file&#8221;][&#8220;tmp_name&#8221;],$_FILES[&#8220;file&#8221;][&#8220;type&#8221;],$_FILES[&#8220;file&#8221;][&#8220;name&#8221;]); \/\/ try adding \/\/ Create a CURLFile object \/ oop method #$cfile = new CURLFile(&#8220;resource\/test.png&#8221;,&#8221;image\/png&#8221;,&#8221;testpic&#8221;); \/\/ uncomment [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":579,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/576"}],"collection":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=576"}],"version-history":[{"count":5,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions\/596"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/media\/579"}],"wp:attachment":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}