{"id":247,"date":"2007-09-19T00:00:00","date_gmt":"2007-09-19T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=247"},"modified":"2007-09-19T00:00:00","modified_gmt":"2007-09-19T00:00:00","slug":"Example PHP File Upload Script","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=247","title":{"rendered":"Example PHP File Upload Script"},"content":{"rendered":"<p><P><SPAN class=mainText>Below shows a PHP file upload script that is written to handle the form data submitted from the earlier <A href=\"http:\/\/www.strongd.net\/blog\/show\/245\"><FONT color=#0000ff>XHTML MP document<\/FONT><\/A>. The PHP script prints out the information obtained from the HTTP request and saves the uploaded file to the &#8220;\/file_uploads&#8221; directory of the WAP server.<\/SPAN><\/P><BR><br \/><DIV><BR><br \/><P><SPAN class=codeText>&lt;?php header(`Content-type: application\/vnd.wap.xhtml+xml`); ?&gt;<BR>&lt;?php echo `&lt;?xml version=&#8221;1.0&#8243;?` . `&gt;`; ?&gt;<BR>&lt;!DOCTYPE html PUBLIC &#8220;-\/\/WAPFORUM\/\/DTD XHTML Mobile 1.0\/\/EN&#8221; &#8220;http:\/\/www.wapforum.org\/DTD\/xhtml-mobile10.dtd&#8221;&gt;<BR><BR>&lt;html xmlns=&#8221;http:\/\/www.w3.org\/1999\/xhtml&#8221;&gt;<BR>&nbsp;&nbsp;&lt;head&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;File Upload Example&lt;\/title&gt;<BR>&nbsp;&nbsp;&lt;\/head&gt;<BR><BR>&nbsp;&nbsp;&lt;body&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;Data Received at the Server&lt;\/h1&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;hr\/&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;<BR><BR>&lt;?php<BR>foreach ($_POST as $key =&gt; $value){<BR>?&gt;<BR><BR>&lt;b&gt;Name-value Pair Info:&lt;\/b&gt;&lt;br\/&gt;<BR>Field name: &lt;?php echo $key; ?&gt;&lt;br\/&gt;<BR>Field value: &lt;?php echo $value; ?&gt;&lt;br\/&gt;&lt;br\/&gt;<BR><BR>&lt;?php<BR>}<BR><BR>$optionalFileName = $_POST[`filename`];<BR><BR>if ($_FILES[`myFile`][`error`] == UPLOAD_ERR_OK){<BR>&nbsp;&nbsp;$fileName = $_FILES[`myFile`][`name`];<BR>?&gt;<BR><BR>&lt;b&gt;Uploaded File Info:&lt;\/b&gt;&lt;br\/&gt;<BR>Content type: &lt;?php echo $_FILES[`myFile`][`type`]; ?&gt;&lt;br\/&gt;<BR>Field name: myFile&lt;br\/&gt;<BR>File name: &lt;?php echo $fileName; ?&gt;&lt;br\/&gt;<BR>File size: &lt;?php echo $_FILES[`myFile`][`size`]; ?&gt;&lt;br\/&gt;&lt;br\/&gt;<BR><BR>&lt;?php<BR>&nbsp;&nbsp;\/* Save the uploaded file if its size is greater than 0. *\/<BR>&nbsp;&nbsp;if ($_FILES[`myFile`][`size`] &gt; 0){<BR>&nbsp;&nbsp;&nbsp;&nbsp;if ($optionalFileName == &#8220;&#8221;)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fileName = basename($fileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fileName = $optionalFileName;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;$dirName = `\/file_uploads\/`;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;if (move_uploaded_file($_FILES[`myFile`][`tmp_name`], $dirName . $fileName)){<BR>?&gt;<BR><BR>&lt;b&gt;The uploaded file has been saved successfully.&lt;\/b&gt;<BR><BR>&lt;?php<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else{<BR>?&gt;<BR><BR>&lt;b&gt;An error occurred when we tried to save the uploaded file.&lt;\/b&gt;<BR><BR>&lt;?php<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>}<BR>?&gt;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/p&gt;<BR>&nbsp;&nbsp;&lt;\/body&gt;<BR>&lt;\/html&gt;<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=mainText>The following screenshots show what you will see on the Nokia 6230 cell phone:<\/SPAN><\/P><BR><br \/><P>&nbsp;<\/P><br \/><TABLE cellSpacing=0 cellPadding=4 width=\"100%\" border=0><br \/><COLGROUP><br \/><COLGROUP><br \/><COL width=256><br \/><THEAD><br \/><TR><br \/><TD vAlign=top width=\"100%\"><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_1967dd40.png\" width=131 align=left border=0 name=image10><BR clear=left><BR><BR><\/P><BR><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m2a245acb.png\" width=131 align=left border=0 name=image11><BR clear=left><BR><BR><\/P><BR><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m49c4f872.png\" width=131 align=left border=0 name=image12><BR clear=left><BR><BR><\/P><BR><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m734eb420.png\" width=131 align=left border=0 name=image13><BR clear=left><BR><BR><\/P><BR><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m3ff8af64.png\" width=131 align=left border=0 name=image14><BR clear=left><BR><BR><\/P><BR><br \/><P><IMG height=130 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_db69705.png\" width=131 align=left border=0 name=image15><BR clear=left><SPAN class=mainText>Nokia 6230 cell phone<\/SPAN><\/P><\/TD><\/TR><\/THEAD><br \/><TBODY><\/TBODY><\/TABLE><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=mainText>The above PHP script is very straightforward. Most of the code has been covered before. Below shows some lines of code that you may be unfamiliar with.<\/SPAN><\/P><BR><br \/><P><SPAN class=mainText>The line:<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=codeText>&lt;?php header(`Content-type: application\/vnd.wap.xhtml+xml`); ?&gt;<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=mainText>is used to set the MIME type of the PHP document. &#8220;application\/vnd.wap.xhtml+xml&#8221; is the MIME type of XHTML MP.<\/SPAN><\/P><BR><br \/><P><SPAN class=mainText>The line:<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=codeText>&lt;?php echo `&lt;?xml version=&#8221;1.0&#8243;?` . `&gt;`; ?&gt;<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=mainText>is used to output the XML declaration &#8220;&lt;?xml version=&#8221;1.0&#8243;?&gt;&#8221;. We have to output it this way since &#8220;&lt;?&#8221; and &#8220;?&gt;&#8221; of the XML declaration are the opening and closing tags of the PHP language. If we include the XML declaration in a PHP document directly, like this:<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=codeText>&lt;?php header(`Content-type: application\/vnd.wap.xhtml+xml`); ?&gt;<BR><B>&lt;?xml version=&#8221;1.0&#8243;?&gt;<\/B><BR>&lt;!DOCTYPE html PUBLIC &#8220;-\/\/WAPFORUM\/\/DTD XHTML Mobile 1.0\/\/EN&#8221; &#8220;http:\/\/www.wapforum.org\/DTD\/xhtml-mobile10.dtd&#8221;&gt;<BR>&#8230;<\/P><BR><br \/><P><SPAN class=mainText>then a parse error will occur.<\/SPAN><\/P><BR><br \/><P><SPAN class=mainText>The line:<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=codeText>$fileName = basename($fileName);<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=mainText>is necessary in the example PHP script since some browsers provide the full path of the uploaded file in the HTTP request, which means $_FILES[`myFile`][`name`] may contain a path but not a file name. Hence, we need to use the PHP function <I>basename()<\/I> to extract the file name from the path. For example, both <I>basename(`\/files\/myFile.txt`)<\/I> and <I>basename(`myFile.txt`)<\/I> returns the string &#8220;myFile.txt&#8221;.<\/SPAN><\/P><BR><br \/><P><SPAN class=mainText>As XHTML MP is compatible with HTML\/XHTML, the resulting XHTML MP document generated by the PHP script can also be viewed on web browsers such as Microsoft Internet Explorer and Mozilla Firefox. The only thing you need to do is to remove the following line from the PHP script:<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=codeText>&lt;?php header(`Content-type: application\/vnd.wap.xhtml+xml`); ?&gt;<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><SPAN class=mainText>This is because unlike WAP 2.0 browsers on cell phones, Internet Explorer 6 and Mozilla Firefox 2.0 do not understand the MIME type of XHTML MP. Instead of displaying the XHTML MP document, they will pop up a dialog box asking you to select a program to open the document or save the document on disk.<\/SPAN><\/P><BR><br \/><P><SPAN class=mainText>The following screenshots show the result on Mozilla Firefox 2.0:<\/SPAN><\/P><BR><br \/><P><BR><\/P><BR><br \/><P><BR><br \/><TABLE cellSpacing=0 cellPadding=4 width=\"100%\" border=0><BR><br \/><COLGROUP><BR><br \/><COLGROUP><br \/><COL width=256><BR><br \/><THEAD><BR><br \/><TR><BR><br \/><TD vAlign=top width=\"100%\"><BR><br \/><P><IMG height=460 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_m54f9ba3.png\" width=478 align=left border=0 name=image24><BR clear=left><BR><BR><\/P><BR><br \/><P><IMG height=460 src=\"http:\/\/www.developershome.com\/wap\/wapUpload\/wap_upload_html_690f6888.png\" width=478 align=left border=0 name=image25><BR clear=left><SPAN class=mainText>Mozilla Firefox 2.0<\/SPAN><\/P><\/TD><\/TR><\/THEAD><BR><br \/><TBODY><\/TBODY><\/TABLE><\/SPAN><\/P><\/DIV><BR><br \/><DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below shows a PHP file upload script that is written to handle the form data submitted from the earlier XHTML MP document. The PHP script prints out the information obtained from the HTTP request and saves the uploaded file to the &#8220;\/file_uploads&#8221; directory of the WAP server.&lt;?php header(`Content-type: application\/vnd.wap.xhtml+xml`); ?&gt;&lt;?php echo `&lt;?xml version=&#8221;1.0&#8243;?` . `&gt;`; &hellip; <a href=\"https:\/\/www.strongd.net\/?p=247\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Example PHP File Upload Script<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=247"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/247\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}