{"id":299,"date":"2022-02-02T22:11:56","date_gmt":"2022-02-02T22:11:56","guid":{"rendered":"https:\/\/ouritsource.com\/blog\/?p=299"},"modified":"2025-05-29T22:40:53","modified_gmt":"2025-05-29T22:40:53","slug":"example-form-to-upload-a-file-using-php","status":"publish","type":"post","link":"https:\/\/ouritsource.com\/blog\/example-form-to-upload-a-file-using-php\/","title":{"rendered":"Example Form to Upload A File Using PHP"},"content":{"rendered":"<body>\n<p>The example is a web page that allows one to select a file from their computer, and have that file uploaded to the webserver.<\/p>\n\n\n\n<p>The first file is  the web page that contains the component to allow one to select a file and the script to run, upload_attachment.php, when the form is submitted.<\/p>\n\n\n\n<p><strong>uploadFile.html<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;HTML&gt;\n&lt;BODY&gt;\n\n&lt;!--- Simple Form to select a file from computer ----&gt;\n&lt;H2&gt; Example of a Web Page to Upload a File &lt;\/H2&gt;\n&lt;form action='upload_attachment.php' method='post'enctype='multipart\/form-data'&gt;\n      Filename  &lt;input type='file' name='uploadedfile'&gt;&lt;\/div&gt;\n      &lt;INPUT TYPE='SUBMIT' Name='Submit' Value='Submit'&gt;\n&lt;\/form&gt;\n\n&lt;\/BODY&gt;\n&lt;\/HTML&gt;\n\n<\/code><\/pre>\n\n\n\n<p><strong> Screenshot of Sample Web Page<\/strong>. When the <strong>Choose File <\/strong>button is clicked, file manager appears to allow one to select a file. As mentioned above, when the <strong>Submit <\/strong>button is clicked, the upload_attachment.php script runs. It contains the script that uploads the selected file to a specific location on the web server.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"305\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2022\/02\/image-7.png?resize=640%2C305&#038;ssl=1\" alt=\"\" class=\"wp-image-570\" style=\"width:1085px;height:auto\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2022\/02\/image-7.png?w=743&amp;ssl=1 743w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2022\/02\/image-7.png?resize=300%2C143&amp;ssl=1 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>upload_attachment.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/generates a random number to be used as part of the filename and store in variable $rand.\n$rand=rand(100,9999999999); \n\n\/\/gets the current month and year and place in variables $month and $year\n$month = date(\"m\");\n$year = date(\"Y\");\n\n\/\/creates a subfolder in <em>docs<\/em> based on the month and year\nmkdir(\"docs\/\".\"$month-$year\",0755,true);\n\n\/\/specifies the location where to upload the file on the web server\n$target_path = \"docs\/$month-$year\/\";\n\n\/\/adds the filename to the $target_path variable\n$target_path = $target_path .$rand. \"-\".basename( $_FILES['uploadedfile']['name']);\n\n\/\/specifies the filename \n$filename = $rand. \"-\".basename( $_FILES['uploadedfile']['name']); \n\n\t\/\/starts uploading file\n        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) \n\t\t{\n\t        \/\/add script to insert filename into a database table if keeping track into a database table\n \t\t}\n?&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>The example is a web page that allows one to select a file from their computer, and have that file uploaded to the webserver. The first file is the web page that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-php","no-thumbnail"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":3,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":571,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions\/571"}],"wp:attachment":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}