{"id":473,"date":"2019-09-13T14:52:12","date_gmt":"2019-09-13T14:52:12","guid":{"rendered":"https:\/\/www.webhozz.com\/code\/?p=473"},"modified":"2019-09-14T04:07:27","modified_gmt":"2019-09-14T04:07:27","slug":"php-file-upload","status":"publish","type":"post","link":"https:\/\/www.webhozz.com\/code\/php-file-upload\/","title":{"rendered":"PHP : File Upload"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">Uploading File dengan PHP<\/h5>\n\n\n\n<p>Dalam tutorial ini kita akan belajar cara mengunggah file di server jauh menggunakan form HTML dan PHP sederhana. Kalian dapat mengunggah segala jenis file seperti gambar, video, file ZIP, dokumen Microsoft Office, PDF, serta file yang dapat dieksekusi dan berbagai jenis file lainnya.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Langkah 1: Membuat formulir HTML untuk mengunggah file<\/h5>\n\n\n\n<p>Contoh berikut akan membuat formulir HTML sederhana yang dapat digunakan untuk mengunggah file.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;File Upload Form&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;form action=&quot;upload-manager.php&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n        &lt;h2&gt;Upload File&lt;\/h2&gt;\n        &lt;label for=&quot;fileSelect&quot;&gt;Filename:&lt;\/label&gt;\n        &lt;input type=&quot;file&quot; name=&quot;photo&quot; id=&quot;fileSelect&quot;&gt;\n        &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Upload&quot;&gt;\n        &lt;p&gt;&lt;strong&gt;Note:&lt;\/strong&gt; Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.&lt;\/p&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<h5 class=\"wp-block-heading\">Langkah 2: Memproses file yang diunggah<\/h5>\n\n\n\n<p>Berikut kode lengkap file kita &#8220;upload-manager.php\u201d. Ini akan menyimpan file yang diunggah dalam folder &#8220;upload&#8221; secara permanen serta menerapkan beberapa pemeriksaan keamanan dasar seperti jenis file dan ukuran file untuk memastikan bahwa pengguna meng-upload jenis file yang benar dan dalam batas yang diizinkan.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/\/ Check if the form was submitted\nif($_SERVER&#x5B;&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;){\n    \/\/ Check if file was uploaded without errors\n    if(isset($_FILES&#x5B;&quot;photo&quot;]) &amp;&amp; $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;error&quot;] == 0){\n        $allowed = array(&quot;jpg&quot; =&gt; &quot;image\/jpg&quot;, &quot;jpeg&quot; =&gt; &quot;image\/jpeg&quot;, &quot;gif&quot; =&gt; &quot;image\/gif&quot;, &quot;png&quot; =&gt; &quot;image\/png&quot;);\n        $filename = $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;name&quot;];\n        $filetype = $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;type&quot;];\n        $filesize = $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;size&quot;];\n    \n        \/\/ Verify file extension\n        $ext = pathinfo($filename, PATHINFO_EXTENSION);\n        if(!array_key_exists($ext, $allowed)) die(&quot;Error: Please select a valid file format.&quot;);\n    \n        \/\/ Verify file size - 5MB maximum\n        $maxsize = 5 * 1024 * 1024;\n        if($filesize &gt; $maxsize) die(&quot;Error: File size is larger than the allowed limit.&quot;);\n    \n        \/\/ Verify MYME type of the file\n        if(in_array($filetype, $allowed)){\n            \/\/ Check whether file exists before uploading it\n            if(file_exists(&quot;upload\/&quot; . $filename)){\n                echo $filename . &quot; is already exists.&quot;;\n            } else{\n                move_uploaded_file($_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;tmp_name&quot;], &quot;upload\/&quot; . $filename);\n                echo &quot;Your file was uploaded successfully.&quot;;\n            } \n        } else{\n            echo &quot;Error: There was a problem uploading your file. Please try again.&quot;; \n        }\n    } else{\n        echo &quot;Error: &quot; . $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;error&quot;];\n    }\n}\n?&gt;\n<\/pre><\/div>\n\n\n<p>Kalian mungkin bertanya-tanya tentang apa kode ini. Baiklah, mari kita lihat setiap bagian dari contoh kode ini satu per satu untuk memahami proses ini dengan lebih baik.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Penjelasan Kode<\/h5>\n\n\n\n<p>Setelah formulir informasi dikirimkan tentang file yang diunggah dapat diakses melalui array superglobal PHP yang disebut <strong><em>$_FILES<\/em><\/strong>. Misalnya, formulir unggahan kita berisi bidang pilih file yang disebut foto (yaitu <strong><em>name=&#8221;photo&#8221;<\/em><\/strong>),jika ada pengguna yang mengunggah file menggunakan cara ini, kita dapat memperoleh detailnya seperti nama, jenis, ukuran, nama sementara atau kesalahan apa pun yang terjadi ketika mencoba mengunggah melalui array asosiatif <strong><em>$_FILES[&#8220;photo&#8221;]<\/em><\/strong>, seperti ini:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><em>$_FILES[&#8220;photo&#8221;][&#8220;name&#8221;]<\/em><\/strong> &#8211; Nilai array ini menentukan nama asli file, termasuk ekstensi file. Itu tidak termasuk file path.<\/li><li><strong><em>$_FILES[&#8220;photo&#8221;][&#8220;type&#8221;]<\/em><\/strong> &#8211; Nilai array ini menentukan tipe MIME dari file.<\/li><li><strong><em>$_FILES[&#8220;photo&#8221;][&#8220;size&#8221;]<\/em><\/strong> &#8211; Nilai array ini menentukan ukuran file, dalam bytes.<\/li><li><strong><em>$_FILES[&#8220;photo&#8221;][&#8220;tmp_name&#8221;]<\/em><\/strong> &#8211; Nilai array ini menentukan nama sementara termasuk full path yang ditugaskan ke file setelah diunggah ke server.<\/li><li><strong><em>$_FILES[&#8220;photo&#8221;][&#8220;error&#8221;] <\/em><\/strong>&#8211; Nilai array ini menentukan kesalahan atau kode status yang terkait dengan unggahan file, mis. itu akan menjadi 0, jika tidak ada error.<\/li><\/ul>\n\n\n\n<p>Kode PHP dalam contoh berikut hanya akan menampilkan detail file yang diunggah dan menyimpannya dalam direktori sementara di server web.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nif($_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;error&quot;] &gt; 0){\n    echo &quot;Error: &quot; . $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;error&quot;] . &quot;&lt;br&gt;&quot;;\n} else{\n    echo &quot;File Name: &quot; . $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;name&quot;] . &quot;&lt;br&gt;&quot;;\n    echo &quot;File Type: &quot; . $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;type&quot;] . &quot;&lt;br&gt;&quot;;\n    echo &quot;File Size: &quot; . ($_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;size&quot;] \/ 1024) . &quot; KB&lt;br&gt;&quot;;\n    echo &quot;Stored in: &quot; . $_FILES&#x5B;&quot;photo&quot;]&#x5B;&quot;tmp_name&quot;];\n}\n?&gt;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Uploading File dengan PHP Dalam tutorial ini kita akan belajar cara mengunggah file di server jauh menggunakan form HTML dan PHP sederhana. Kalian dapat mengunggah<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-473","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Belajar PHP : File Upload - WebHozz Code<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webhozz.com\/code\/php-file-upload\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Belajar PHP : File Upload - WebHozz Code\" \/>\n<meta property=\"og:description\" content=\"Uploading File dengan PHP Dalam tutorial ini kita akan belajar cara mengunggah file di server jauh menggunakan form HTML dan PHP sederhana. Kalian dapat mengunggah\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webhozz.com\/code\/php-file-upload\/\" \/>\n<meta property=\"og:site_name\" content=\"WebHozz Code\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-13T14:52:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-14T04:07:27+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\"},\"headline\":\"PHP : File Upload\",\"datePublished\":\"2019-09-13T14:52:12+00:00\",\"dateModified\":\"2019-09-14T04:07:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/\"},\"wordCount\":324,\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/\",\"name\":\"Belajar PHP : File Upload - WebHozz Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\"},\"datePublished\":\"2019-09-13T14:52:12+00:00\",\"dateModified\":\"2019-09-14T04:07:27+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/php-file-upload\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP : File Upload\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#website\",\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/\",\"name\":\"WebHozz Code\",\"description\":\"Tutorial Web &amp; Pemrograman Indonesia\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/#\\\/schema\\\/person\\\/3b2b79dc317236b0dde4b1fda37263e1\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\\\/\\\/www.webhozz.com\\\/code\\\/author\\\/dody\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Belajar PHP : File Upload - WebHozz Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webhozz.com\/code\/php-file-upload\/","og_locale":"en_US","og_type":"article","og_title":"Belajar PHP : File Upload - WebHozz Code","og_description":"Uploading File dengan PHP Dalam tutorial ini kita akan belajar cara mengunggah file di server jauh menggunakan form HTML dan PHP sederhana. Kalian dapat mengunggah","og_url":"https:\/\/www.webhozz.com\/code\/php-file-upload\/","og_site_name":"WebHozz Code","article_published_time":"2019-09-13T14:52:12+00:00","article_modified_time":"2019-09-14T04:07:27+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webhozz.com\/code\/php-file-upload\/#article","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/php-file-upload\/"},"author":{"name":"admin","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1"},"headline":"PHP : File Upload","datePublished":"2019-09-13T14:52:12+00:00","dateModified":"2019-09-14T04:07:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webhozz.com\/code\/php-file-upload\/"},"wordCount":324,"articleSection":["PHP"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.webhozz.com\/code\/php-file-upload\/","url":"https:\/\/www.webhozz.com\/code\/php-file-upload\/","name":"Belajar PHP : File Upload - WebHozz Code","isPartOf":{"@id":"https:\/\/www.webhozz.com\/code\/#website"},"datePublished":"2019-09-13T14:52:12+00:00","dateModified":"2019-09-14T04:07:27+00:00","author":{"@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1"},"breadcrumb":{"@id":"https:\/\/www.webhozz.com\/code\/php-file-upload\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webhozz.com\/code\/php-file-upload\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.webhozz.com\/code\/php-file-upload\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webhozz.com\/code\/"},{"@type":"ListItem","position":2,"name":"PHP : File Upload"}]},{"@type":"WebSite","@id":"https:\/\/www.webhozz.com\/code\/#website","url":"https:\/\/www.webhozz.com\/code\/","name":"WebHozz Code","description":"Tutorial Web &amp; Pemrograman Indonesia","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webhozz.com\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.webhozz.com\/code\/#\/schema\/person\/3b2b79dc317236b0dde4b1fda37263e1","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f222cb0ed38f2100d666bb262fd38d4f0d8e5673698208e40ff83118f10a4e8e?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/www.webhozz.com\/code\/author\/dody\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/comments?post=473"}],"version-history":[{"count":1,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/473\/revisions"}],"predecessor-version":[{"id":474,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/posts\/473\/revisions\/474"}],"wp:attachment":[{"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/media?parent=473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/categories?post=473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webhozz.com\/code\/wp-json\/wp\/v2\/tags?post=473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}